Jump to content

Regex for selecting information


-Karl-

Recommended Posts

If I am understanding your question correctly and looking at the source returned by the curl function, you're trying to parse the following lines:

<img src="http://www.runescape.com/img/hiscores/skill_icon_Attack1.gif" alt="Attack"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Defence1.gif" alt="Defence"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Strength1.gif" alt="Strength"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Constitution1.gif" alt="Constitution"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Ranged1.gif" alt="Ranged"/>92
<img src="http://www.runescape.com/img/hiscores/skill_icon_Prayer1.gif" alt="Prayer"/>80
<img src="http://www.runescape.com/img/hiscores/skill_icon_Magic1.gif" alt="Magic"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Cooking1.gif" alt="Cooking"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Woodcutting1.gif" alt="Woodcutting"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Fletching1.gif" alt="Fletching"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Fishing1.gif" alt="Fishing"/>99
<img src="http://www.runescape.com/img/hiscores/skill_icon_Firemaking1.gif" alt="Firemaking"/>86
<img src="http://www.runescape.com/img/hiscores/skill_icon_Crafting1.gif" alt="Crafting"/>87
<img src="http://www.runescape.com/img/hiscores/skill_icon_Smithing1.gif" alt="Smithing"/>83
<img src="http://www.runescape.com/img/hiscores/skill_icon_Mining1.gif" alt="Mining"/>85
<img src="http://www.runescape.com/img/hiscores/skill_icon_Herblore1.gif" alt="Herblore"/>81
<img src="http://www.runescape.com/img/hiscores/skill_icon_Agility1.gif" alt="Agility"/>75
<img src="http://www.runescape.com/img/hiscores/skill_icon_Thieving1.gif" alt="Thieving"/>76
<img src="http://www.runescape.com/img/hiscores/skill_icon_Slayer1.gif" alt="Slayer"/>81
<img src="http://www.runescape.com/img/hiscores/skill_icon_Farming1.gif" alt="Farming"/>83
<img src="http://www.runescape.com/img/hiscores/skill_icon_Runecrafting1.gif" alt="Runecrafting"/>72
<img src="http://www.runescape.com/img/hiscores/skill_icon_Hunter1.gif" alt="Hunter"/>70
<img src="http://www.runescape.com/img/hiscores/skill_icon_Construction1.gif" alt="Construction"/>70
<img src="http://www.runescape.com/img/hiscores/skill_icon_Summoning1.gif" alt="Summoning"/>1
<img src="http://www.runescape.com/img/hiscores/skill_icon_Dungeoneering1.gif" alt="Dungeoneering"/>9

to get the string in the "alt" attribute and the number following the tag. All those lines are similar enough that a regular expression should be able to work. I suggest you post a question in the regular expression subsection and ask how to do it.

 

Ken

 

I have my code: http://www.phpfreaks.com/forums/index.php/topic,297661.0.html

 

There is no problem with the code itself, it works fine. I'm just trying to find another way to shorten it and to make it more efficient. Does anyone have any ideas?

 

What I'm trying to do is select the numbers after the alt tag. Following a number is an </a> each time.

Link to comment
Share on other sites

Array ( [0] => Array ( [0] => alt="Attack"/>99 [1] => alt="Defence"/>99 [2] => alt="Strength"/>99 [3] => alt="Constitution"/>99 [4] => alt="Ranged"/>92 [5] => alt="Prayer"/>80 [6] => alt="Magic"/>99 [7] => alt="Cooking"/>99 [8] => alt="Woodcutting"/>99 [9] => alt="Fletching"/>99 [10] => alt="Fishing"/>99 [11] => alt="Firemaking"/>86 [12] => alt="Crafting"/>87 [13] => alt="Smithing"/>83 [14] => alt="Mining"/>85 [15] => alt="Herblore"/>81 [16] => alt="Agility"/>75 [17] => alt="Thieving"/>76 [18] => alt="Slayer"/>81 [19] => alt="Farming"/>83 [20] => alt="Runecrafting"/>72 [21] => alt="Hunter"/>70 [22] => alt="Construction"/>70 [23] => alt="Summoning"/>1 [24] => alt="Dungeoneering"/>9 ) [1] => Array ( [0] => Attack [1] => Defence [2] => Strength [3] => Constitution [4] => Ranged [5] => Prayer [6] => Magic [7] => Cooking [8] => Woodcutting [9] => Fletching [10] => Fishing [11] => Firemaking [12] => Crafting [13] => Smithing [14] => Mining [15] => Herblore [16] => Agility [17] => Thieving [18] => Slayer [19] => Farming [20] => Runecrafting [21] => Hunter [22] => Construction [23] => Summoning [24] => Dungeoneering ) [2] => Array ( [0] => 99 [1] => 99 [2] => 99 [3] => 99 [4] => 92 [5] => 80 [6] => 99 [7] => 99 [8] => 99 [9] => 99 [10] => 99 [11] => 86 [12] => 87 [13] => 83 [14] => 85 [15] => 81 [16] => 75 [17] => 76 [18] => 81 [19] => 83 [20] => 72 [21] => 70 [22] => 70 [23] => 1 [24] => 9 ) ) 

 

That's what's printed with a simple test, is it supposed to be like that?

 

Printing the alts, then the stats name, then the level?

Link to comment
Share on other sites

Yes, the print_r statement was just an example so that you knew what the $out array contained. You now have an array of stat names in $out[1] and an array of values in $out[3]. You can then combine the two if you wanted.

 

$stats = array_combine($out[1], $out[2]);
print_r($stats);

Link to comment
Share on other sites

Oh that's perfect. Now to figure out how to utilize this in a MySQL query.

 

foreach($stats as $name => $value) {
    echo $name . ': ' . $value . ' <br/>';
}

 

That gives the output of

 

Attack: 99

Strength: 99

 

How would I use this in a MySQL query?

 

My current query is like this:

$adventurer = $db->query("UPDATE `members` SET

`attack` = '$attack',

`strength` = '$strength',

 

Well you get the idea.

Link to comment
Share on other sites

Depending on your settings, you could use something like this.

 

$values = array();
foreach($stats as $name => $value) {
    $values[] = "{$name}='$value'";
}
$adventurer = $db->query("UPDATE `members` SET " . implode(',', $values)); 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.