[PHP] file reading and regex

2003-06-04 Thread Michael Allan
New to the list, so I hope I'm in the right place.

I'm trying to write a small php function which checks a page filled 
with a list of satellite images of earth, and loads the latest one to 
have been taken at 4:25 in the morning, when the view is most complete 
(full credit to be given to the original source, of course ... I'm not 
pretending to be running a satellite here).

Here's what I've got so far:

?php

 	$URLbase = 
http://rsd.gsfc.nasa.gov/goesg/earth/Weather/GMS-5/jpg/vis/4km/;;
 	
 	if (!($fp = fopen($URLbase, r)))
 	{
 		echo p style=color:whiteCould not open the url/p;
 		exit;
 	}
 	$contents = fread($fp, 100);
 	fclose($fp);
 	
	$pattern = (^[[:digit:]]{6}0425\.jpg$);
 	if (eregi($pattern, $contents, $fullviews))
 	{
 		$latestfullview = end($fullviews);
 		echo img src=\.$URLbase.$latestfullview.\ width=\470\
 			  . alt=\Earth from the GMS-5 satellite above the Timor strait\ 
\\;
 	}
 	else
 	{
 		echo p style=color:whiteNo image available/p;
 	}
 ?

The error I'm getting points to the fclose($fp) line, but whether or 
not that's the culprit I don't know. (What little java I've written has 
taught me that the effects of an error can be registered far from it's 
source - perhaps the url isn't properly formed for the fopen() 
function?)

I've tested the regex, and it returns true for a jpg file named with a 
10 digit number ending in 0425 (eg. 0305110425.jpg) as it should, but 
not for eg. a href=0305110425.jpg0305110425.jpg/a, which is the 
sort of code in which the file name is embedded in the page I'm asking 
it to search, so I guess there's a problem here. Am I understanding the 
caret and dollar signs correctly, as respectively indicating the start 
and end of the search string? Or do they refer to the token being 
searched?

I want to add more functionality to the script yet (load a default 
image and email the webmaster when no image is found, date and time of 
the image capture, etc.) but I need to get at least this much working 
to start off with.

Any suggestions? I'm still new to this, so feel free to mention the 
most obvious mistakes as well as any more subtle ones.
I'm working locally on Mac OS X if that makes any difference.

Many thanks,
Mike
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] file reading and regex

2003-06-04 Thread Michael Allan
Marek,

Your pattern should look like:
$pattern = ([[:digit:]]{6}0425\.jpg);
BINGO!

This will give you the image name, but remember that ereg works on 
single lines and also the image might be taken at some time around 
4:25. So you need a while loop to read the html line by line, in the 
loop try to match the pattern, and if the pattern succeeds (first few 
lines and last 2 lines are just html junk), check if it is close 
enough to 4:25. Then break the while loop and display the image.
I'll look into this - 04:25 has been a constant for the months I've 
been watching this, but I'll work out a routine to check if this 
changes as you suggest. And thanks for the explanation of ereg() - that 
will help.

And Jay,

Was that really a line break after $URLbase = ? (Probably not, but just
checking) ...
nah, just line wrapping in the email

Try adding a 'b' to your fopen ...

fopen($URLbase, rb)

... in the event that you're dealing with a Windows platform.
I presume from the forward slashes in $URLbase that I'm dealing with 
just such a platform, so I've added the b (and looked up what it 
means). Good tip.

Thanks for such quick and effective help, guys! What a great resource.

Mike


Michael Allan wrote:
New to the list, so I hope I'm in the right place.
I'm trying to write a small php function which checks a page filled 
with a list of satellite images of earth, and loads the latest one to 
have been taken at 4:25 in the morning, when the view is most 
complete (full credit to be given to the original source, of course 
... I'm not pretending to be running a satellite here).
Here's what I've got so far:
?php
 $URLbase = 
http://rsd.gsfc.nasa.gov/goesg/earth/Weather/GMS-5/jpg/vis/4km/;;
 if (!($fp = fopen($URLbase, r)))
 {
 echo p style=color:whiteCould not open the url/p;
 exit;
 }
 $contents = fread($fp, 100);
 fclose($fp);
$pattern = (^[[:digit:]]{6}0425\.jpg$);


 if (eregi($pattern, $contents, $fullviews))
 {
 $latestfullview = end($fullviews);
 echo img src=\.$URLbase.$latestfullview.\ 
width=\470\
   . alt=\Earth from the GMS-5 satellite above the 
Timor strait\ \\;
 }
 else
 {
 echo p style=color:whiteNo image available/p;
 }
 ?
The error I'm getting points to the fclose($fp) line, but whether or 
not that's the culprit I don't know. (What little java I've written 
has taught me that the effects of an error can be registered far from 
it's source - perhaps the url isn't properly formed for the fopen() 
function?)
I've tested the regex, and it returns true for a jpg file named with 
a 10 digit number ending in 0425 (eg. 0305110425.jpg) as it should, 
but not for eg. a href=0305110425.jpg0305110425.jpg/a, which is 
the sort of code in which the file name is embedded in the page I'm 
asking it to search, so I guess there's a problem here. Am I 
understanding the caret and dollar signs correctly, as respectively 
indicating the start and end of the search string? Or do they refer 
to the token being searched?
I want to add more functionality to the script yet (load a default 
image and email the webmaster when no image is found, date and time 
of the image capture, etc.) but I need to get at least this much 
working to start off with.
Any suggestions? I'm still new to this, so feel free to mention the 
most obvious mistakes as well as any more subtle ones.
I'm working locally on Mac OS X if that makes any difference.
Many thanks,
Mike



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php