Preg_match help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitr021
Forum Newbie
Posts: 4
Joined: Sat Nov 13, 2004 10:19 am

Preg_match help

Post by nitr021 »

i am trying to grab some stats of some site and i am having problem with the preg_match. i am just new to preg_match and i have spent two days on it and i can't still figure how to grab this number 1,846 on line 5

Code: Select all

<tbody><tr><td colspan="3" width="120"><b>Skill</b></td><td align="right" width="80"><b>Rank</b></td><td align="right" width="80"><b>Level</b></td><td align="right" width="80"><b>XP</b></td></tr>
<tr><td><img src="http://www.runescape.com/img/title/blank.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=0" class="c">Overall</a></td>
<td align="right">1</td><td align="right">
1,846  
</td><td align="right">
231,001,751
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/attack.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=1" class="c">Attack</a></td>

<td align="right">110</td><td align="right">
99
</td><td align="right">
13,356,134
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/defence.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=2" class="c">Defence</a></td>
<td align="right">167</td><td align="right">
99
</td><td align="right">
13,037,556
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/strength.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>

<td><a href="hiscoreuser.cgi?username=zezima&category=3" class="c">Strength</a></td>
<td align="right">268</td><td align="right">
99
</td><td align="right">
13,203,212
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/hitpoints.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=4" class="c">Hitpoints</a></td>
<td align="right">57</td><td align="right">
99
</td><td align="right">
16,381,915
</td></tr>

<tr><td><img src="http://www.runescape.com/img/hiscores/ranged.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=5" class="c">Ranged</a></td>
<td align="right">130</td><td align="right">
99
</td><td align="right">
13,075,739
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/prayer.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=6" class="c">Prayer</a></td>
<td align="right">72</td><td align="right">
83
</td><td align="right">
2,719,273

</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/magic.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=7" class="c">Magic</a></td>
<td align="right">137</td><td align="right">
99
</td><td align="right">
13,141,120
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/cooking.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=8" class="c">Cooking</a></td>
<td align="right">122</td><td align="right">
99

</td><td align="right">
13,857,621
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/woodcutting.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=9" class="c">Woodcutting</a></td>
<td align="right">45</td><td align="right">
99
</td><td align="right">
13,260,246
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/fletching.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=10" class="c">Fletching</a></td>
<td align="right">91</td><td align="right">

99
</td><td align="right">
13,156,075
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/fishing.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=11" class="c">Fishing</a></td>
<td align="right">73</td><td align="right">
99
</td><td align="right">
13,077,300
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/firemaking.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=12" class="c">Firemaking</a></td>
<td align="right">56</td><td align="right">

99
</td><td align="right">
13,062,700
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/crafting.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=13" class="c">Crafting</a></td>
<td align="right">9</td><td align="right">
98
</td><td align="right">
12,215,730
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/smithing.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=14" class="c">Smithing</a></td>
<td align="right">9</td><td align="right">

99
</td><td align="right">
16,909,110
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/mining.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=15" class="c">Mining</a></td>
<td align="right">34</td><td align="right">
99
</td><td align="right">
13,059,926
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/herblore.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=16" class="c">Herblore</a></td>
<td align="right">18</td><td align="right">

90
</td><td align="right">
5,760,437
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/agility.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=17" class="c">Agility</a></td>
<td align="right">9</td><td align="right">
99
</td><td align="right">
13,061,708
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/thieving.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=18" class="c">Thieving</a></td>
<td align="right">49</td><td align="right">

99
</td><td align="right">
13,088,641
</td></tr>
<tr><td><img src="http://www.runescape.com/img/hiscores/runecrafting.gif" valign="bottom" height="16" width="16"></td><td>&nbsp;</td>
<td><a href="hiscoreuser.cgi?username=zezima&category=21" class="c">Runecrafting</a></td>
<td align="right">4</td><td align="right">
90
</td><td align="right">
5,577,308
</td></tr>
</tbody>
i am using this to connect

Code: Select all

<?php

$user = "zezima";
$handle = fopen("http://hiscore.runescape.com:80/aff/runescape/hiscorepersonal.cgi?username=$user", "r")
   or exit("Unable to open hiscore table");
while (!feof($handle)) {
   $buffer = fgets($handle, 4096);

   
   preg_match('/^<td>[^<a.*>]Overall[^<\/a>|<\/td>]<td.*>(.*)<\/td><td.*>(.?)<\/td>/',$buffer, $out);
   echo $out[0];
}

fclose($handle);
?>
i am trying to get it to say 1846 only

can some please help
Last edited by nitr021 on Sat Nov 13, 2004 5:52 pm, edited 1 time in total.
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

Whats $stats on line 10?

I'm not sure if preg_match ONLY works for single lines, but it's definitely a better idea to only pass it a single line at a time.

Split your input up into individual lines, try and identify the line before the number you want, then you don't even have to pattern match the next line as all it is is the number you're looking for.
nitr021
Forum Newbie
Posts: 4
Joined: Sat Nov 13, 2004 10:19 am

Post by nitr021 »

hmm never thought of that
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

will the numbers ALWAYS be on thier own line?

if so, theres no need for regex

Code: Select all

$lines = file($url);

foreach ($lines as $line) {
    if (is_numeric(trim($line))) {
     // got it
    }
}
nitr021
Forum Newbie
Posts: 4
Joined: Sat Nov 13, 2004 10:19 am

Post by nitr021 »

yh i think so
nitr021
Forum Newbie
Posts: 4
Joined: Sat Nov 13, 2004 10:19 am

Post by nitr021 »

ok i got this

Code: Select all

<?php

$user = "nitr021";
$handle = fopen("http://hiscore.runescape.com:80/aff/runescape/hiscorepersonal.cgi?username=$user", "r")
   or exit("Unable to open hiscore table");
while (!feof($handle)) {
   $buffer = fgets($handle, 4096);

   
 
 
 if (is_numeric(trim($buffer))) {
echo $buffer;

    }
	
}

fclose($handle);
?>
and i get this

Code: Select all

80 75 80 79 65 49 71 67 88 84 67 57 59 71 70 48 56 51 56
but when i try to substr($buffer, 1,2);
i dn't get 80 i get some jumble of numbers.
when i view teh source it is like this

Code: Select all

80
75
80
79
65
49
71
67
88
84
67
57
59
71
70
48
56
51
56

how would i got to change the script to show

Code: Select all

80 75 80 79 65 49 71 67 88 84 67 57 59 71 70 48 56 51 56
so i can do substr on it
Post Reply