Specifications
FIGURE 17.1
The script uses a regular expression to pull out the stock quote from information retrieved from NASDAQ.
The script itself is pretty straightforward—in fact, it doesn’t use any functions we haven’t seen
before, just new applications of those functions.
You might recall that when we discussed reading from files in Chapter 2, “Storing and
Retrieving Data,” we mentioned that you could use the file functions to read from an URL.
That’s what we have done in this case. The call to fopen()
$fp = fopen($theurl, “r”)
returns a pointer to the start of the page at the URL we supply. Then it’s just a question of
reading from the page at that URL and closing it again:
$contents = fread($fp, 1000000);
fclose($fp);
You ’ll notice that we used a really large number to tell PHP how much to read from the file.
With a file on the server, you’d normally use filesize($file), but this doesn’t work with
an URL.
When we’ve done this, we have the entire text of the Web page at that URL stored in
$contents. We can then use a regular expression and the eregi() function to find the part
of the page that we want:
$pattern = “(\\\$[0-9 ]+\\.[0-9]+)”;
if (eregi($pattern, $contents, $quote))
{
echo “$symbol was last sold at: “;
echo $quote[1];
}
That’s it!
Using Network and Protocol Functions
C
HAPTER 17
17
USING NETWORK
AND
PROTOCOL
FUNCTIONS
373
22 7842 CH17 3/6/01 3:39 PM Page 373










