tags:

views:

21

answers:

1

Hello,

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:

<img src="http://l.yimg.com/a/i/us/we/52/11.gif"/&gt;&lt;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"&gt;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:

  • the link from img src in the first line
  • number of degress in the third line (before C)

Thank you.

+2  A: 

This will get the img source:

src="(.*?)"

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

And this will get the degrees:

.*?, (\d+) C

Again, just get the first group.

$input = '<img src="http://l.yimg.com/a/i/us/we/52/11.gif"/&gt;&lt;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"&gt;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];
SimpleCoder
@SimpleCoder: an example would be great. Thanks!
Psyche
It works, thank you very much!
Psyche
Sure, no problem
SimpleCoder