Jump to content

PREG_MATCH_ALL - Help!


tejama

Recommended Posts

I'm trying to write code to scrape the content of a weather page to get today's forecast using preg_match_all.  Below is the code I'm using that is generating the error:

 

function get_weather() {
$html = file_get_contents("http://weatheroffice.gc.ca/city/pages/nl-24_metric_e.html");

preg_match_all("/<dt>Today<\/dt><dd>(.*)<\/dd>/sm", $html, $forecast, PREG_SET_ORDER);

echo $forecast[0][0];
}

 

The piece of html content I'm trying to scrape is as follows:

 

<dt>Today</dt>
<dd>Freezing rain mixed with ice pellets changing to rain and ending near noon then cloudy. Wind becoming northeast 30 km/h gusting to 50 this morning then southwest 50 gusting to 70 near noon. High plus 4.</dd>

 

Basically the error I'm getting is that preg_match_all is returning no results.  Can anyone point out where I might be going wrong in my regex?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/258315-preg_match_all-help/
Share on other sites

you are not accounting for the spaces/newline after the closing <dt> tag and before the opening <dd> tag.

 

$html = "<dt>Today</dt>
<dd>Freezing rain mixed with ice pellets changing to rain and ending near noon then cloudy. Wind becoming northeast 30 km/h gusting to 50 this morning then southwest 50 gusting to 70 near noon. High plus 4.</dd>";
preg_match_all("/<dt>Today<\/dt>\s*<dd>([^<]+)<\/dd>/i", $html, $forecast, PREG_SET_ORDER);
echo "<pre>"; print_r($forecast); echo "</pre>";

 

results:

 

Array
(
    [0] => Array
        (
            [0] => 
Today

Freezing rain mixed with ice pellets changing to rain and ending near noon then cloudy. Wind becoming northeast 30 km/h gusting to 50 this morning then southwest 50 gusting to 70 near noon. High plus 4.

            [1] => Freezing rain mixed with ice pellets changing to rain and ending near noon then cloudy. Wind becoming northeast 30 km/h gusting to 50 this morning then southwest 50 gusting to 70 near noon. High plus 4.
        )

)

Link to comment
https://forums.phpfreaks.com/topic/258315-preg_match_all-help/#findComment-1324116
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.