PHP Str_replace 图像类型
我有一个应用程序的 XML 天气提要。该提要正在发送 .GIF 图像的信息,但我希望它查找 .PNG。我尝试了 STR_REPLACE 但没有成功。
$icon = $xml->xpath("/xml_api_reply/weather/forecast_conditions/icon");
$iconData = str_replace('.gif','.png',$icon);
echo '<img src="'.get_bloginfo('stylesheet_directory').'/images'.$iconData[2]->attributes().'" />';
更多信息 :::
XML 提要不提供图像。只是他们的 URL 的开头。因此 $icon[1]
的 XML Feed 的输出是 ig/images/weather/mostly_sunny.gif
。然后我在开头添加了我们的 URL 并设置了相同的路径,但我只需要
在第一行 ig/images/weather/mostly_sunny.gif 之后将 .gif 更改为 .png ECHO $icon[1] 仅此而已。
I have an XML weather feed for an application. The feed is sending out information for .GIF images but I want it to look for .PNG. I tried a STR_REPLACE but it did not work.
$icon = $xml->xpath("/xml_api_reply/weather/forecast_conditions/icon");
$iconData = str_replace('.gif','.png',$icon);
echo '<img src="'.get_bloginfo('stylesheet_directory').'/images'.$iconData[2]->attributes().'" />';
A little more info :::
The images are not provided by the XML feed. Just the beginning of the URL for them. So the output from the XML Feed for $icon[1]
say is ig/images/weather/mostly_sunny.gif
. I have then added our URL at the beginning and set up the same path but I just need the .gif to change to .png
ECHO $icon[1] after the first line is ig/images/weather/mostly_sunny.gif
That is all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里发生了很多事情,很难预测会发生什么。
此时,
$icon
是 SimpleXMLObject 对象的数组(可能为空)。str_replace
可以接受数组作为第三个参数,它可能还可以将$icon
中的值强制转换为字符串。其结果取决于 XML 的结构,如果icon
元素始终是文本,则应该没问题。这到底有用吗?我本以为此时 $iconData 将是一个字符串数组,而不是一个 SimpleXMLObject 对象数组。
如果我是您,我会手动迭代 xpath 搜索的结果,并在需要将 SimpleXMLObject 对象表现为字符串时将它们显式转换为字符串。
HTH。
You've got lots of stuff going on here, it's a hard to predict what might happen.
At this point,
$icon
is an array, possibly empty, of SimpleXMLObject objects.str_replace
can accept an array as the third argument, it's possible that it also coerces the values in$icon
in to strings. The result of this is dependent on the structure of your XML, if theicon
elements are always text this should be OK.Does this work at all? I would have thought that, at this point, $iconData would be an array of strings, not an array of SimpleXMLObject objects.
If I were you I'd manually iterate the results of the xpath search and explicitly cast SimpleXMLObject objects to strings as an when I needed them to behave as strings.
HTH.
试试这个:
那么您确定您这边的文件夹是“stylesheet_directory”加上 /images 加上 /ig/images/weather 吗?
我认为最初的问题是 str_replace 用于字符串,而您传入了一个数组?..
Try this:
So you're sure the folder on your side is this 'stylesheet_directory' PLUS /images PLUS the /ig/images/weather ?
I think the original problem is that str_replace is for strings and you passed in an array?..