|
Posted by s on 03/29/06 08:41
On 26 Mar 2006 15:51:54 -0800, "Anthony David Adams"
<anthony@175g.com> wrote:
>I've been trying to write this off and on for a while now, and in the
>process, have been trying to figure out preg_match. This is a very
>simple, straightforward problem..
>
>I am writing a peice of PHP to read in a webpage that changes each
>week, and pull out the ranking for a college ultimate frisbee team.
>
>I have no trouble reading the webpage into string variable, but I am
>uncertain on the proper preg_match syntax to extract the rank (and
>eventually other information such as record.
>
>The end goal is to be able to pass a team name to the script, and have
>it return the rank and record of the team.. For now, I am just focused
>on getting the rank.
>
>The raw page is here :
>http://www4.upa.org/scores/scores.cgi?div=18&page=22
<?php
$page =
file_get_contents('http://www4.upa.org/scores/scores.cgi?div=18&page=22');
$teams = array();
if
(preg_match_all("/href=.scores.cgi\?div=(\d+)&page=(\d+)&team=(\d+).\'><td
align=right>(\d+)\) <td align=center>(-?\d+)<td><u>(.*?)<td
align=center>(\d+-\d+)<td align=center>(.*?)<td align=center>(.*?)/",
$page, $matches)) {
for ($i=0; $i < count($matches[4]); $i++) {
$teams[$matches[6][$i]] = array('rank' => $matches[4][$i], 'pr' =>
$matches[5][$i], 'record' => $matches[7][$i]);
}
}
echo 'Wisconsin ranks ' . $teams['Wisconsin']['rank'] . "\n";
echo 'Colorado has a record of ' . $teams['Colorado']['record'] .
"\n";
?>
That will probably break horribly with the linewraps, perhaps enough
so that the regex no longer works. If you hit a snag, grab it from
here instead:
http://shaunc.com/phps/frisbeeScores.phps
Just access a team's stats using
$stats['Teamname']['stat']
...where 'stat' is one of 'rank' 'pr' or 'record' .. Or you can
probably figure how to pull the other stats, like region or section,
into the $stats array (play with the $matches[8] and $matches[9]
arrays).
Currently, I get the following output:
$ php frisbeeScores.php
Wisconsin ranks 2
Colorado has a record of 12-5
If this script is going to be called often, I'd suggest caching the
results to a database or a text file in order to prevent having to
load the remote page every time your script is loaded.
hth
--
<s@guerril.la.ape> (to email, remove .ape)
Apologies for the ad below.
--
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Navigation:
[Reply to this message]
|