GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Mar-2004, 10:23
cave monkey cave monkey is offline
New Member
 
Join Date: Mar 2004
Location: Juneau, Alaska
Posts: 7
cave monkey is on a distinguished road

file retrieval....reg expression?


I am trying to pull a weather report from a remote html page, and would like to know how to gather just the report ( not the whole page) for display on my own page. I have succesfully pulled the whole page into mine using the following code:
PHP Code:

@ $fp = fopen("http://url/index.html", "r");
if (!fp)
{
echo 'Data Temporarily Unanvailable!';
exit;
}
while (!feof($fp))
{
$weather = fgets($fp, 999);
echo $weather;
} 


I have tried matching patterns with eregi() but without success, as the text of the weather report is different each day. However, the location of the text in the page is consistent, and it is clearly delineated with unique start and end points. How do I get my file pointer there, and not pull in the whole page? The url I'm retrieving from is here: http://pajk.arh.noaa.gov/index.php?fcst=AKZ/AKZ025/html/all.html
And all I want is the text of the report, between the "FPAK77PAJK" and the "$$". The are so many file functions that I'm overwhelmed. Can anyone recommend a logical way to approach this? Thanks in advance. DM
  #2  
Old 24-Mar-2004, 12:53
cave monkey cave monkey is offline
New Member
 
Join Date: Mar 2004
Location: Juneau, Alaska
Posts: 7
cave monkey is on a distinguished road

Solution found!


A user on another forum provided me with a solution, using <pre> and </pre> tags that exist in the page, and surround the weather report.
PHP Code:

$str = file_get_contents("http://url");
preg_match("|<pre>(.+)</pre>|si", $str, $m);
 echo nl2br($m[0]); 


....worked like a charm, if anyone cares.
  #3  
Old 25-Mar-2004, 04:52
erniegerdie erniegerdie is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: England
Posts: 93
erniegerdie has a spectacular aura abouterniegerdie has a spectacular aura about
Or if your not too up to date on your reg expr's and you just wanted the data inbetween the tags, without the <pre> tags you could use a more generic function, i.e:

PHP Code:

function grabtagdata( $searchstring, $starttag, $endtag ) {
    $endpos = 0;
    $tempstring = strtolower($searchstring); // set string to lowercase for a case insensitive search
    $offset = strlen($starttag);
  while ($startpos = strpos($tempstring, $starttag, $endpos)) {
                $startpos += $offset; // add $offset to remove $starttag from grabbed data
                $endpos = strpos($tempstring, $endtag, $startpos);
                $matchedstring .= substr($searchstring, $startpos, ($endpos-$startpos));    //grab the data inbetween the tags
  }
  return $matchedstring; 
}

$str = file_get_contents("http://pajk.arh.noaa.gov/index.php?fcst=AKZ/AKZ025/html/all.html");
echo nl2br(grabtagdata($str, "<pre>", "</pre>")); //make sure to send tags that are lowercase 



You could also have a variation of this function which spat the matched strings into an array and then return the array if need be.

If my code is at all dodgy pls feel free to correct me.
  #4  
Old 25-Mar-2004, 11:37
cave monkey cave monkey is offline
New Member
 
Join Date: Mar 2004
Location: Juneau, Alaska
Posts: 7
cave monkey is on a distinguished road

Thanks.


Thanks, that worked just as well. I'm just learning this stuff, but have quickly realized there are 101 ways to skin a cat in PHP. Thanks for the code, and I look forward to trying some variations of it in other situations. Thanks again. DM
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
C++ file I/O CronoX C++ Forum 36 09-Mar-2004 18:28
read specified range of lines from file etron C Programming Language 8 18-Feb-2004 09:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.