Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.