Re: preg_match issues...



On 26 Mar 2006 15:51:54 -0800, "Anthony David Adams"
<anthony@xxxxxxxx> 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@xxxxxxxxxxxxxx> (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 ***
.



Relevant Pages

  • Re: [PHP] PHP console script vs C/C++/C#
    ... My script is taking a longer time to execute than I want. ... I prefer to write in PHP because that is what I know best. ... This is why I am thinking about rewriting my whole script in a C language. ... Perhaps there are different methods I could be using to speed up execution. ...
    (php.general)
  • Re: How to Add a Feeback Form
    ... I saw nothing in that script that indicates where the form is e-mailed to so ... Greg Maxey/Word MVP ... PHP or not. ... have the support available yet. ...
    (microsoft.public.frontpage.programming)
  • How best to show PHP source? (was: One page, multiple submit buttons)
    ... script to add to the top of a PHP script to enable showing its ... Maybe anybody submitting their own PHP code for critique here ... Anything posted to a newsgroup is a "snapshot" of what the ... there ought to be a FAQ for this ...
    (comp.lang.php)
  • Re: [PHP] PHP console script vs C/C++/C#
    ... My script is taking a longer time to execute than I want. ... I prefer to write in PHP because that is what I know best. ... thinking about rewriting my whole script in a C language. ... execution of the code. ...
    (php.general)
  • Email form script
    ... I have a web form that posts to a .php ... who cannot run .php on their server. ... Or does anyone have a .asp script that will ... Thank you for choosing FormToEmail by FormToEmail.com ...
    (microsoft.public.scripting.jscript)