<?php
$url = 'http://www.weatheroffice.ec.gc.ca/city/pages/on-11_metric_e.html';
$handle = fopen($url, "r");
$weather = file_get_contents($url);
fclose($handle);
/**
*
* Clean up the retrieved HTML code
*
* We clean up the source we got here. Basically, we only want a section
* of the code. We only need the working weather section. So we cut out
* the meat, starting at '<div id="citybanneradright" class="right">' and
* ending at '<div id="cityadditional">' which will give us just the info
* about the current conditions. Exactly what we want!!
*
*/
$weather = substr($weather, strpos($weather, '<div id="citybanneradright" class="right">'), (strpos($weather,'<div id="cityadditional">')-strpos($weather, '<div id="citybanneradright" class="right">')));
$weather = substr($weather, strpos($weather, '<h1>'), ((strpos($weather, '</dl>')-strpos($weather, '<h1>'))+5));
/**
* PERL RegEx Pattern to match
*
* The pattern of what the HTML will look like each time that page is hit
* by us (or any other viewer). We define areas with parenthesis "()"
* and what their values can contain with ranges "[a-zA-Z]".
* Research Regular Expressions if you don't understand.
*
*/
$pattern = "@<h1>
<a name=\"content\"> </a>([-\s\w]*)</h1>
<div id=\"city\">
<div id=\"box\">
<div id=\"citycurrent\">
<h2>Currently</h2>
<div id=\"cityobserved\">([-\w\s\:]*)</div>
<dl>
<dt>Temperature</dt>
<dd>([- \d]*) °([F|C]{1})</dd>
<dd class=\"dd1\"/>
<dt>Pressure/ Tendency</dt>
<dd>([- \d\.]*) kPa<span class=\"[- \w]*\" id=\"[- \w]*\">([&;\w]*)</span></dd>
<dd class=\"dd1\"/>
<dt>Humidity</dt>
<dd>([\d]*) %</dd>
<dd class=\"dd1\"/>
<dt>Dewpoint</dt>
<dd>([-\d]*) °([F|C]{1})</dd>
<dd class=\"dd1\"/>
<dt>Wind</dt>
<dd>([ \w\n\/\t]*)</dd>
<dd class=\"dd1\"/>
</dl>@";
preg_match($pattern, $weather, $info);
preg_match_all($pattern, $weather, $info2);
?>
<pre>
<?php
var_dump($info);
echo '<hr>';
var_dump($info2);
?>
</pre>
That's what I came up with. For some odd reason, the RegEx doesn't work, not sure why. If you put it in a RegEx checker (like the one from the makers of PHPEdit), it works perfectly!! But I couldn't get my local server (PHP5, Apache 2) to do it. I could go line-by-line with regex, but that was too tedius. preg_match_all should work with this pattern, but for some odd reason, it doesn't. Maybe someone else can figure it out.
$me->Racking_his_brain_as_he_goes_to_get_drunk(...)
~Brett