Click to See Complete Forum and Search --> : Pulling dynamically changing information from a game site
Shears
07-05-2006, 08:33 PM
I would like to pull certain information from a game onto my website. (I hope "pull" is the correct word.) Player rank information for this game can be seen here (http://www.kingsofchaos.com/battlefield.php?start=0). For example, with this (http://www.kingsofchaos.com/stats.php?id=1683506) person - at this very moment, one of the user stats of MidnightRain is "Army Size: 1,423,073"
I would like to pull this number, which constantly changes, onto my site.
Essentially, in the end, i would like my site to displays, certain information about particular users, in a list.
Eg.
Army size of X = 455,959
Army size of Y = 50,595
Army size of Z = 59,599,394
I hope this makes sense, and thank you for any help :)
Shears :)
NogDog
07-05-2006, 09:45 PM
This will load all the info from that table into an array, from which you can then extract whatever data you want to display. Just copy-and-paste it into a .php file and run it, and you'll see how the data is stored in the $data array.
<?php
$html = file_get_contents("http://www.kingsofchaos.com/battlefield.php?start=0");
if($html !== FALSE)
{
$s = '[\s\r\n]*'; // whitespace/new line
$regex = '|<tr[^>]*>'.$s.'<td><a[^>]*>([^<]*)</a></td>'.
''.$s.'<td[^>]*>([^<]*)</td>'.$s.'<td[^>]*>'.$s.'([^<]*)'.$s.'</td>'.
''.$s.'<td[^>]*>([^<]*)</td>'.$s.
'<td[^>]*>([^<]*)</td>'.$s.'</tr>|Us';
preg_match_all($regex, $html, $matches);
$items = array(1 => 'name',
2 => 'army',
3 => 'race',
4 => 'gold',
5 => 'rank');
for($ix = 0; $ix < count($matches[1]); $ix++)
{
foreach($items as $key => $cat)
{
$data[$ix][$cat] = trim($matches[$key][$ix]);
}
}
// show data:
echo "<pre>";
print_r($data);
echo "</pre>\n";
}
?>
Shears
07-06-2006, 07:24 PM
Thank you NogDog! That was just what i wanted :)
$regex = '|<tr[^>]*>'.$s.'<td><a[^>]*>([^<]*)</a></td>'.
''.$s.'<td[^>]*>([^<]*)</td>'.$s.'<td[^>]*>'.$s.'([^<]*)'.$s.'</td>'.
''.$s.'<td[^>]*>([^<]*)</td>'.$s.
'<td[^>]*>([^<]*)</td>'.$s.'</tr>|Us';I have one question. What is the significance of writing Us at the end of the $regex ?
Thank you again :)
Shears :)
NogDog
07-06-2006, 07:35 PM
U = toggles to Ungreedy mode, so that the shortest part of the string that matches is selected instead of the longest
s = Doesn't seem to actually stand for anything, but the purpose is to have the "." wildcard character match newlines as well as any other character (by default it does not match newlines)
More info at: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php