开发者

PHP Str_replace image type

开发者 https://www.devze.com 2023-03-07 23:35 出处:网络
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.

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 XM开发者_C百科L 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.


You've got lots of stuff going on here, it's a hard to predict what might happen.

$icon = $xml->xpath("/xml_api_reply/weather/forecast_conditions/icon");

At this point, $icon is an array, possibly empty, of SimpleXMLObject objects.

$iconData = str_replace('.gif','.png',$icon);

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 the icon elements are always text this should be OK.

echo '<img src="'.get_bloginfo('stylesheet_directory').'/images'.$iconData[2]->attributes().'" />'

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.


Try this:

$iconData = str_replace('.gif','.png',$icon[1]);
echo '<img src="'.get_bloginfo('stylesheet_directory').'/images/'.$iconData.'" />'

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?..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号