...

View Full Version : Reading value off another page



fbelzile
11-03-2009, 05:56 PM
So I'm trying to get the weather off of the Environment Canada page using this script:


<?php
$page = file_get_contents('http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html');
preg_match('/<p class="temperature"/>.*?/<\sup/>/',$page,$match);

echo $match[1];
?>

I just don't know how this: '/<p class="temperature"/>.*?/<\sup/>/' works. (Or doesn't work.) Specifically, to get the number value for the temperature and the condition (such as: Rain, Snow, Fog, Variable Cloudiness, ect...)

Thanks for reading.

Phil Jackson
11-03-2009, 07:33 PM
<?php
$page = file_get_contents('http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html');

if(preg_match("#<p class=\"temperature\">([^<]*)#is", $page, $match))
{
echo $match[1];
}
else
{
echo "no find temp";
}
?>



EDIT: Just changed it

fbelzile
11-03-2009, 10:41 PM
Thanks for the code, but it only gives "no find temp"
I'll fiddle around with it a bit more... Is there no online resource that has a tutorial on this that will explain all the uses of brackets, slashes and stars?

Anyways, the webpage is posted here:
http://fbelzile.000a.biz/index2.php

Phil Jackson
11-03-2009, 11:07 PM
I've tested the above code and it does retrieve the temperature

fbelzile
11-04-2009, 04:46 AM
I shouldn't have doubted you! It works, I had to host the file myself with php installed. The host I was using claimed it didn't disable any functions (even mail).

Again thank you very much. I'm still interested on how that jumble works if anyone can find a tutorial to explain it.

Have a great day.

shadowmaniac
11-04-2009, 04:55 AM
Regular expressions are a pain but very powerful and useful if you use it correctly.
Found a couple sites that may be helpful:
http://gnosis.cx/publish/programming/regular_expressions.html
http://www.zytrax.com/tech/web/regex.htm
or you can always google for regular expressions.

fbelzile
11-04-2009, 07:30 PM
See below, using new script.

fbelzile
11-05-2009, 01:51 PM
I'm now using Curl as it is available on the hosted php server. Here is my code, it can retrieve both the temperature and the id. But now, I'm displaying a little weather icon depending on what the id is. The page is blank when I go to it, so I'm assuming theres something wrong with the PHP;



<?php
$url = "http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html";
$ch = curl_init();//Initialise CURL
curl_setopt($ch, CURLOPT_URL, $url);//Set the url
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);//We want it to return data
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 100);//Of course, we don't want your script to run forever, so set a timeout
$text = curl_exec($ch);//Execute and get the page

$exploded = explode('<p class="temperature">',$text);//Make an array of the part before and after the <title> tag
$result = explode('<sup>',$exploded[1]);//Grab the part after the <title> tag, and split that too

$exploded2 = explode('<img id="currentimg" src="/weathericons/',$text);//Make an array of the part before and after the <title> tag
$result2 = explode('.gif"',$exploded2[1]);//Grab the part after the <title> tag, and split that too

curl_close($ch);//Close cURL

if($result2[0] < "02"){
echo "<img src=\"images/weather/00.gif\" alt=\"Sunny\">";
}
if(($result2[0] > "01") && ($result2[0] < "09")) {
echo "<img src=\"images/weather/01.gif\" alt=\"Cloudy with chance of showers\">";
}
if($result2[0] == "09" || $result2[0] == "19" || $result2[0] == "39") {
echo "<img src=\"images/weather/03.gif\" alt=\"Storm\">";
}
if(($result2[0] > "9" && $result2[0] < "9") || ($result2[0] > "19" && $result2[0] < "25") || ($result2[0] > "26" && $result2[0] < "30") || ($result2[0] > "31" && $result2[0] < "39")) {
echo "<img src=\"images/weather/02.gif\" alt=\"Cloudy with chance of showers\">";
}
if(($result2[0] > "24" && $result2[0] < "27") || ($result2[0] > "29" && $result2[0] < "32") || ($result2[0] > "39") {
echo "";
}
else{
echo "";
}
echo $result[0];//Echo the result
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum