解析来自Yahoo的某些信息!天气RSS提要

时间:2021-06-16 19:35:54

I'm using Yahoo! Weather RSS Feed in order to get the forecast for my town. I can parse the XML and get weather description, but then I get a result like this:

我使用雅虎天气RSS频道,以便为我的城镇预报。我可以解析XML并得到天气描述,但是我得到的结果如下:

<img src="http://l.yimg.com/a/i/us/we/52/11.gif"/><br />
<b>Current Conditions:</b><br />
Light Rain, 18 C<BR />
<BR /><b>Forecast:</b><BR />
Tue - PM Thundershowers. High: 25 Low: 16<br />
Wed - Rain. High: 23 Low: 17<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Constanta__RO/*http://weather.yahoo.com/forecast/ROXX0034_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>

My regex skills are almost null, so I'm asking for some help to parse the following info:

我的regex技能几乎为空,因此我请求一些帮助来解析以下信息:

  • the link from img src in the first line
  • img src的链接在第一行
  • number of degress in the third line (before C)
  • 第三行(C)前的出入口数

Thank you.

谢谢你!

1 个解决方案

#1


3  

This will get the img source:

这将得到img的来源:

src="(.*?)"

src = "(. * ?)”

Just get the first group (the region between the parentheses)

得到第一组(圆括号之间的区域)

And this will get the degrees:

这将得到学位:

.*?, (\d+) C

. * ?(\ d +)C

Again, just get the first group.

再一次,得到第一组。

$input = '<img src="http://l.yimg.com/a/i/us/we/52/11.gif"/><br />'.
"<b>Current Conditions:</b><br />".
"Light Rain, 18 C<BR />".
"<BR /><b>Forecast:</b><BR />".
"Tue - PM Thundershowers. High: 25 Low: 16<br />".
"Wed - Rain. High: 23 Low: 17<br />".
"<br />".
'<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Constanta__RO/*http://weather.yahoo.com/forecast/ROXX0034_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>'.
'(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>';

$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $input, $matches);
$imgsrc = $matches[1];

$degpattern = '/.*?, (\d+) C/i';
preg_match($degpattern, $input, $matches);
$degs = $matches[1];

#1


3  

This will get the img source:

这将得到img的来源:

src="(.*?)"

src = "(. * ?)”

Just get the first group (the region between the parentheses)

得到第一组(圆括号之间的区域)

And this will get the degrees:

这将得到学位:

.*?, (\d+) C

. * ?(\ d +)C

Again, just get the first group.

再一次,得到第一组。

$input = '<img src="http://l.yimg.com/a/i/us/we/52/11.gif"/><br />'.
"<b>Current Conditions:</b><br />".
"Light Rain, 18 C<BR />".
"<BR /><b>Forecast:</b><BR />".
"Tue - PM Thundershowers. High: 25 Low: 16<br />".
"Wed - Rain. High: 23 Low: 17<br />".
"<br />".
'<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Constanta__RO/*http://weather.yahoo.com/forecast/ROXX0034_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>'.
'(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>';

$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $input, $matches);
$imgsrc = $matches[1];

$degpattern = '/.*?, (\d+) C/i';
preg_match($degpattern, $input, $matches);
$degs = $matches[1];