2010-06-22 4 views
2

J'utilise Yahoo! Météo Flux RSS afin d'obtenir les prévisions pour ma ville. Je peux analyser le XML et obtenir la description du temps, mais j'obtenir un résultat comme celui-ci:Analyse de certaines informations de Yahoo! Météo Flux RSS

<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/> 

Mes compétences regex sont presque nulles, donc je demande un peu d'aide pour analyser les informations suivantes:

  • le lien de img src dans la première ligne
  • nombre de degress dans la troisième ligne (avant C)

Merci.

Répondre

3

Cela obtiendra la source img:

src="(.*?)"

Procurez-vous le premier groupe (la région entre les parenthèses)

Et cela obtenir les degrés:

.*?, (\d+) C

Encore une fois, obtenez le premier groupe.

$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]; 
+0

un exemple serait génial. Merci! – Psyche

+0

Cela fonctionne, merci beaucoup! – Psyche

+0

Bien sûr, pas de problème –