Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Wyciąganie danych ze stringa
Forum PHP.pl > Forum > Przedszkole
northwest
Witam serdecznie,
Mam następujący skrypt:


  1.  
  2. $zipcode = 'UKXX0014';
  3.  
  4. $result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=c');
  5. $xml = new DOMDocument();
  6. $xml->loadXML($result);
  7. $xpath = new DOMXPath($xml);
  8.  
  9. echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
  10.  
  11. $location = $xpath->query('channel/yweather:location')->item(0);
  12.  
  13. if(!empty($location)){
  14. $items = $xpath->query('channel/item');
  15. foreach($items as $item){
  16. $current = $xpath->query('yweather:condition', $item)->item(0);
  17. $forecast = $xpath->query('yweather:forecast', $item);
  18. $output = <<<END
  19.   <h1 style="margin-bottom: 0">Weather for {$location->getAttribute('city')}, {$location->getAttribute('region')}</h1>
  20.   <small>{$current->getAttribute('date')}</small>
  21.   <h2>Current Conditions</h2>
  22.   <p>
  23.   <span style="font-size:72px; font-weight:bold;">{$current->getAttribute('temp')}&deg;F</span>
  24.   <br/>
  25.   <img src="http://l.yimg.com/a/i/us/we/52/{$current->getAttribute('code')}.gif" style="vertical-align: middle;"/>&nbsp;
  26.   {$current->getAttribute('text')}
  27.   </p>
  28.   <h2>Forecast</h2>
  29.   {$forecast->item(0)->getAttribute('day')} - {$forecast->item(0)->getAttribute('text')}. High: {$forecast->item(0)->getAttribute('high')} Low: {$forecast->item(0)->getAttribute('low')}
  30.   <br/>
  31.   {$forecast->item(1)->getAttribute('day')} - {$forecast->item(1)->getAttribute('text')}. High: {$forecast->item(1)->getAttribute('high')} Low: {$forecast->item(1)->getAttribute('low')}
  32.   </p>
  33. END;
  34. }
  35.  



  1. Zwraca mi następujące dane:
  2. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  3. <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
  4. <channel>
  5.  
  6. <title>Yahoo! Weather - Bedford, UK</title>
  7. <link>http://us.rd.yahoo.com/dailynews/rss/weather/Bedford__UK/*http://weather.yahoo.com/forecast/UKXX0014_c.html</link>
  8. <description>Yahoo! Weather for Bedford, UK</description>
  9. <language>en-us</language>
  10. <lastBuildDate>Wed, 02 Jul 2014 6:19 pm BST</lastBuildDate>
  11. <ttl>60</ttl>
  12. <yweather:location city="Bedford" region="" country="UK"/>
  13. <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
  14. <yweather:wind chill="21" direction="230" speed="11.27" />
  15. <yweather:atmosphere humidity="53" visibility="9.99" pressure="1015.92" rising="0" />
  16. <yweather:astronomy sunrise="4:44 am" sunset="9:25 pm"/>
  17. <image>
  18. <title>Yahoo! Weather</title>
  19. <width>142</width>
  20. <height>18</height>
  21. <link>http://weather.yahoo.com</link>
  22. <url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
  23. </image>
  24. <item>
  25. <title>Conditions for Bedford, UK at 6:19 pm BST</title>
  26. <geo:lat>52.13</geo:lat>
  27. <geo:long>-.46</geo:long>
  28. <link>http://us.rd.yahoo.com/dailynews/rss/weather/Bedford__UK/*http://weather.yahoo.com/forecast/UKXX0014_c.html</link>
  29. <pubDate>Wed, 02 Jul 2014 6:19 pm BST</pubDate>
  30. <yweather:condition text="Partly Cloudy" code="30" temp="21" date="Wed, 02 Jul 2014 6:19 pm BST" />
  31. <description><![CDATA[
  32. <img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
  33. <b>Current Conditions:</b><br />
  34. Partly Cloudy, 21 C<BR />
  35. <BR /><b>Forecast:</b><BR />
  36. Wed - Partly Cloudy. High: 23 Low: 12<br />
  37. Thu - Sunny. High: 26 Low: 13<br />
  38. Fri - Partly Cloudy. High: 27 Low: 14<br />
  39. Sat - Showers. High: 22 Low: 11<br />
  40. Sun - Scattered Showers. High: 21 Low: 9<br />
  41. <br />
  42. <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Bedford__UK/*http://weather.yahoo.com/forecast/UKXX0014_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
  43. (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
  44. ]]></description>
  45. <yweather:forecast day="Wed" date="2 Jul 2014" low="12" high="23" text="Partly Cloudy" code="29" />
  46. <yweather:forecast day="Thu" date="3 Jul 2014" low="13" high="26" text="Sunny" code="32" />
  47. <yweather:forecast day="Fri" date="4 Jul 2014" low="14" high="27" text="Partly Cloudy" code="30" />
  48. <yweather:forecast day="Sat" date="5 Jul 2014" low="11" high="22" text="Showers" code="11" />
  49. <yweather:forecast day="Sun" date="6 Jul 2014" low="9" high="21" text="Scattered Showers" code="39" />
  50. <guid isPermaLink="false">UKXX0014_2014_07_06_7_00_BST</guid>
  51. </item>
  52. </channel>
  53. </rss>
  54.  
  55. <!-- api8.weather.bf1.yahoo.com Wed Jul 2 15:59:52 PDT 2014 -->
  56.  


chciałbym wyciągnąć z powyższego tekstu:
$dzisiajmin - dzisiejsza temperatura minimalna, $dzisiajmax - dzisiejsza temperatura maksymalna
$dzien1min - jutrzejsza temperatura minimalna, $dzien1max - jutrzejsza temperatura maksymalna
$dzien2min - temperatura minimalna za 2 dni, $dzien2max - temperatura maksymalna za 2 dni
i tak do 5 dnia smile.gif

Wie ktoś może jak to zrobić?

Bardzo proszę o pomoc,
Łukasz
Pyton_000
preg_match

Kod
^<yweather.*?day="(\w{3})".*?low="(\d+)".*high="(\d+)".*?\/>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.