繁体   English   中英

返回URL而不是图像-使用PHP正则表达式从XML提取数据

[英]returning URL instead of image - pulling data from XML using PHP regular expression

我有一个名为get_current_weather_data()的函数,该函数基本上是从Yahoo XML提要中获取天气的。 我可以返回我喜欢的所有值,例如位置,温度和当前条件。 我无法获取与当前状况相关的图像。

这是我试图拉入图像的区域:

// get weather icon url 
$description = $xml->channel->item->description;

//preg match regular expression to extract weather icon
$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $description, $matches);

$weather['icon_url'] = $matches[1];
echo $weather['icon_url'];

上面的代码返回的只是图像URL,而不是图像。 任何想法?

供稿网址:

http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f

或者,如果您愿意的话,可以输入上述要提取的上述URL中的一个片段:(请注意我当前要提取的img src)

<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br /> <b>Current Conditions:</b><br     /> Fair, 40 F<BR /> <BR /><b>Forecast:</b><BR /> Tue - Sunny. High: 44 Low: 32<br /> Wed -     Partly Cloudy. High: 47 Low: 39<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/*http://weather.yahoo.com/forecast/USNY0996_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>

]]>

get_current_weather_data()函数:

function get_current_weather_data() {

// Get XML data from source
$feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f");

// Check to ensure the feed exists
if (!$feed) {
    die('Weather not found! Check feed URL');
}

$xml = new SimpleXmlElement($feed);

$weather['city'] = $xml->channel->children('yweather', TRUE)->location->attributes()->city;
echo $weather['city'] . "<br />";

$weather['temp'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->temp;
echo $weather['temp'] . "<br />";

$weather['conditions'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->text;
echo $weather['conditions'] . "<br />";

// get weather icon url 
$description = $xml->channel->item->description;

//preg match regular expression to extract weather icon
$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $description, $matches);

$weather['icon_url'] = $matches[1];
echo $weather['icon_url'];

return $weather;
}

如果要获取实际的图像数据,可以使用

file_get_contents($weather['icon_url']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM