Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Reading value off another page

    So I'm trying to get the weather off of the Environment Canada page using this script:

    PHP Code:
    <?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.

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?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
    Last edited by Phil Jackson; 11-03-2009 at 06:38 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    fbelzile (11-03-2009)

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    I've tested the above code and it does retrieve the temperature
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    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...pressions.html
    http://www.zytrax.com/tech/web/regex.htm
    or you can always google for regular expressions.

  • #7
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts
    See below, using new script.
    Last edited by fbelzile; 11-05-2009 at 04:07 PM.

  • #8
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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 Code:
    <?php  
        $url 
    "http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html";  
        
    $ch curl_init();//Initialise CURL  
        
    curl_setopt($chCURLOPT_URL$url);//Set the url  
        
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);//We want it to return data  
        
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT100);//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 
        
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •