SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 34
  1. #1
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Reading from a .txt file

    Hi!

    I am looking to supply a weather feed which needs specific info (such as wind speed, visibility, etc.) for a model aeroplane flying club so I am looking at building my own weather feed.

    I would like to use data from the Bureau of Meteorology using their anonymous FTP server and a couple of .txt files containing the data I require.

    What I would like to know is, is it possible to read specific parts of a .txt file to display in a web page? i.e. Not all of it, just something like "windspeed 18km".

    If it is possible, how would I go about doing it?

    Thanks!
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parsing files is fun. Give me a sample of what you'll be dealing with, and I'll tell you how hard it will be to extract what you want.

  3. #3
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is an example of one of them...ftp://ftp2.bom.gov.au/anon/gen/fwo/IDW12100.txt

    Still looking for a .txt file with the wind details. Can the same be done with an .html file?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well...this line of code will grab the contents of a file, but it's impossible to help you further without knowing exactly what you're after.
    PHP Code:
    $file implode(''file('ftp://ftp2.bom.gov.au/anon/gen/fwo/IDW12100.txt')); 

  5. #5
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, sorry - I should have been more specific.

    In the example file I gave, how would I go about retrieving the minimum and maximum temperature for Sunday?

    Thanks for the initial code...I appreciate it.
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alexandra
    In the example file I gave, how would I go about retrieving the minimum and maximum temperature for Sunday?
    PHP Code:
    $file implode(''file('ftp://ftp2.bom.gov.au/anon/gen/fwo/IDW12100.txt'));
    preg_match_all('/sunday[^\d]+(\d{2})[^\d]+(\d{2})/i'$file$matches);
    echo 
    'Min: '$matches[1][0], ' Max: '$matches[2][0]; 

  7. #7
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much!

    Would you be able to explain what the "preg_match_all" line actually does?

    I'm new to PHP and I've never seen this sort of code before and I like to understand what is going on before I start.

    Your help is really appreciated. :-)
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_match_all takes a minimum of 3 parameters. The first is simply a regular expression, which I'll explain later. The second is the string you want to search through, which in this case is the contents of your file that we read using file. The last parameter is an array of matched patterns found in the string from the previous parameter.

    Here's a breakdown of the regex (if it looks intimidating it's because it is! ):
    Code:
    /               # signals the start of the pattern
        sunday      # simply searches for the word 'sunday'
        [^          # the start of a character class where we should match
                    #     everything *except for* the characters inside the []
            \d      # \d is shorthand for saying "digit"...
                    #     ...and we want to match characters that aren't digits
        ]           # the end of a character class
        +           # shorthand for saying "one or more of whatever is behind it"
        (           # the start of a subpattern, which allows us to refer back
                    #     to whatever was matched inside the subpattern later on
            \d      # this looks familiar...
            {2}     # says "find exactly 2 of whatever is behind it"
        )           # ends the subpattern
        [^\d]+      # 1 or more characters that are not digits
        (\d{2})     # a subpattern of exactly 2 digits
    /               # signals the end of the pattern...
    i               #     ...which will be case-insensitive, thanks to the i
                    #     without the i, 'sunday' would have to be 'Sunday'
    So there you have it...nice and simple.

  9. #9
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! I think I'll go off and have a play around with this now. I must say it's a lot easier than I expected it to be.
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One note: there are 2 syntaxes for regular expressions, POSIX and PCRE. You definitely want to learn PCRE syntax since it's faster and more widely-used. If you really want to get into regular expressions, grab a copy of Mastering Regular Expressions by O'Reilly. Also, I just stumbled across this app called The Regex Coach, which looks pretty interesting; you might give it a try.

  11. #11
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again! :-) I'll definately check out those links.

    You've been so helpful, I don't want to overstep the mark, but I have one more questions.

    The example above was a relatively easy one. To get the windspeed, I need to access this document: http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDY03028.txt

    How would I go about it as the data is arranged in columns and isn't so easy to isolate?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  12. #12
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would go through it line by line (don't implode the array). Then I would strip off the name and explode the rest with $data = explode(' ', $str). So then you got yourself a nice array in which you can get the wind with $data[6].

  13. #13
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gaheris,

    Thanks for the reply...I'm new to this so I don't really understand how to go about implementing your proposal. Could you please explain in more detail?

    Thanks :-)
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  14. #14
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do this, it works, I tried it.
    PHP Code:
    $f 'http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDY03028.txt';
    $a implode(''file($f));
    if (
    preg_match('/<pre>(.*?)<\/pre>/is'$a$m))
        
    $a explode("\n"$m[1]);

    $r = array();
    foreach (
    $a as $line) {
        if (
    preg_match('/^([A-Z][a-z]+ (?:[a-zA-Z]+)?)/'$line$match)) {
            
    $t array_filter(array_map('trim'explode(' '
                 
    substr($linestrlen($match[1])))), 
                 
    create_function('$v''if (strlen($v) > 0) return true; else return false;'));
            
    $r[trim($match[1])] = $t;    
        }
    }

    echo 
    '<pre>';
    print_r($r);
    echo 
    '</pre>'

  15. #15
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much! It retrieved the info perfectly.

    To print out only the wind speed for Pearce, how would I do that?

    Would I use a for or while loop?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  16. #16
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print_r($r['Pearce']); 

  17. #17
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, ok. Always simpler than you expect it to be. So to print the 11th entry inthe array I would put

    PHP Code:
    print_r($r['Pearce[11]']); 
    Or have I got it completely wrong? I've never printed a specific part of an array before...
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  18. #18
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo $r['Pearce'][11]; 

  19. #19
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! You have been very helpful, I really appreciate it! :-D
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  20. #20
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome, I'm glad I could help you.

  21. #21
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gaheris: I'll cover the night shift and you can cover the day shift. Good tag team.

  22. #22
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mattjacob: Yeah, you did a good job answering the first question. I like your regex explanation.

  23. #23
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    We have a problem...

    Hi Guys,

    It seems we have a problem...The code was working perfectly yesterday, but when I came to test it today it was displaying no data.

    So I did the following:

    1. Changed the file ref to the version the FTP site (as it should be).
    2. Checked the version on the FTP server to ensure data existed.
    3. Put in the print_r statement to see what information was retrieved.

    The results I found were:

    1. The data does exist on the FTP server.
    2. The print_r statement is not returning any data.

    Because I'm not sure exactly what all the pieces of code do, I can't figure out what has gone wrong.

    Any ideas?

    Here is the final code again if it helps:

    PHP Code:
    <table class="weatherBox">
     <tr>
         <th class="weatherBox">WEATHER REPORT</th>
     </tr>
     <tr>
         <td class="weatherBox">
             <?php
                 $f 
    'ftp://ftp2.bom.gov.au/anon/gen/fwo/IDY03028.txt';
                 
    $a implode(''file($f));
                 if (
    preg_match('/<pre>(.*?)<\/pre>/is'$a$m))
                     
    $a explode("\n"$m[1]);
                 
                 
    $r = array();
                 foreach (
    $a as $line) {
                     if (
    preg_match('/^([A-Z][a-z]+ (?:[a-zA-Z]+)?)/'$line$match)) {
                         
    $t array_filter(array_map('trim'explode(' ',
                              
    substr($linestrlen($match[1])))),
                              
    create_function('$v''if (strlen($v) > 0) return true; else return false;'));
                         
    $r[trim($match[1])] = $t;    
                     }
                 }
             
                 echo 
    '<p>';
                 echo 
    '<b>Currently:</b> ' $r['Pearce'][12] . '&deg;C<br>';
                 echo 
    '<b>Wind:</b> ' $r['Pearce'][11] . ' kts<br>';
                 echo 
    '<b>Visibility:</b> ' $r['Pearce'][9] . ' km<br>';
                 echo 
    '</p>';
             
    ?>
         </td>
     </tr>
     </table>
     
     <?php
         
    echo '<pre>';
         
    print_r($r['Pearce']);
         echo 
    '</pre>';
     
    ?>
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  24. #24
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the working (well, it was working) example of the code.....http://www.cadmium.com.au/weather-test.php
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  25. #25
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Replace
    PHP Code:
    $f 'ftp://ftp2.bom.gov.au/anon/gen/fwo/IDY03028.txt';
    $a implode(''file($f));
    if (
    preg_match('/<pre>(.*?)<\/pre>/is'$a$m))
        
    $a explode("\n"$m[1]); 
    with
    PHP Code:
    $a file('ftp://ftp2.bom.gov.au/anon/gen/fwo/IDY03028.txt'); 
    The problem was that the original data source was HTML with a the weather data in a pre element. But then you used the original text only file, so I had to delete that part which matched the HTML.


Bookmarks

Posting Permissions

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