php pattern matching
Posted by: MajorGeek
Posted on: 2006-12-05 13:53:00
Ok, I got past my problem with file_get_contents (http://discussion.dreamhost.com/showthreaded.pl?Cat=&Board=forum_programming&Number=62841) by using curl instead. But I can't seem to figure out pattern matching in eregi. I'd just like to pull the dates and flows into arrays to plot with my data, but in this test script I don't seem to be able to grab the flows with anything I've tried for $pattern. Is it egregi or something else?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Prosser Flow</title>
</head>
<body>
<?php
$theurl="http://www.usbr.gov/pn-bin/yak/arc3.pl"
."?station=YRPW&year=2006&month=4&day=1&year=2006&month=7&day=31&pcode=QD";
// if (!($contents = file_get_contents($theurl)))
//{
// echo 'Could not open URL';
// exit;
// }
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $theurl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
// display file
echo $contents;
// find the part of the page we want and output it
//$pattern = '([0-9]+.[0-9]+)';
// $pattern = '(2122.08)';
//$pattern = '(^[0-9]+.[0-9]+$)';
//$pattern = '[[:digit:]]{4}.[[:digit:]]{2}';
$pattern = '^[[:digit:]]{4}.[[:digit:]]{2}$';
if (eregi($pattern, $content, $flow))
{
echo "<p>$flow is: ";
echo $flow[1];
echo '</p>';
}
else
{
echo '<p>Nothing matched</p>';
};
?>
</body>
</html>
This signature line intentionally blank.