Jump to content

Recommended Posts

Well, I'm trying to create a script to grab information from another source, however, I currently use substr and strpos, which is pretty inefficient.

 

What I'm hoping to do is use Regex, but I'm completely useless with it. I can't get my head around it. So, say you search for an item, the output (view source) would be something like this:

 

 

// First Item
<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=5354" alt="Plant pot"></td> //Contains the item id and the name
<td><a href="http://services.runescape.com/m=itemdb_rs/Plant_pot/viewitem.ws?obj=5354"> Plant pot</a></td> //Contains the link to more specific information on the item
<td>199</td> //Contains the current price
<td><span class="stay">0</span></td> //contains the change in price

//Second Item
<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=4440" alt="Pot lid"></td> //Contains the item id and the name
<td><a href="http://services.runescape.com/m=itemdb_rs/Pot_lid/viewitem.ws?obj=4440"> Pot lid</a></td> //Contains the link to more specific information on the item
<td>9</td>  //Contains the current price
<td><span class="stay">0</span></td> //contains the change in price

So the lines are pretty constant.

 

<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=5354" alt="Plant pot"></td>

From the above line, I need to grab the 5354, and the Plant pot. As that is the item id and the item name.

 

<td><a href="http://services.runescape.com/m=itemdb_rs/Plant_pot/viewitem.ws?obj=5354"> Plant pot</a></td>

From this line, I need to url, to carry out another function to grab more information from that link.

 

<td>199</td>

This is the current price of the item in gold pieces.

 

<td><span class="stay">0</span></td>

This is the change in price, the class is a variable, either rise, fall, or stay and the number is the change in price. which will either be +195gp, +14.5k, +1.3m, etc, or it will be a - (minus) with the same formats.

 

Any help with this? I've had a crack but I can't figure it out.

Link to comment
https://forums.phpfreaks.com/topic/203536-preg_match_all/
Share on other sites

<<?php
$items = <<<ITEM
// First Item
<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=5354" alt="Plant pot"></td> //Contains the item id and the name
<td><a href="http://services.runescape.com/m=itemdb_rs/Plant_pot/viewitem.ws?obj=5354"> Plant pot</a></td> //Contains the link to more specific information on the item
<td>199</td> //Contains the current price
<td><span class="stay">0</span></td> //contains the change in price

//Second Item
<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=4440" alt="Pot lid"></td> //Contains the item id and the name
<td><a href="http://services.runescape.com/m=itemdb_rs/Pot_lid/viewitem.ws?obj=4440"> Pot lid</a></td> //Contains the link to more specific information on the item
<td>9</td>  //Contains the current price
<td><span class="stay">0</span></td> //contains the change in price
ITEM;

preg_match_all('~<td><a href="(.*)">(.*)</a></td>~iU', $items, $pot_matches);
preg_match_all('~<td>([0-9]+)</td>~', $items, $price_matches);
preg_match_all('~<td><span class="stay">([0-9]+)</span></td>~', $items, $change_matches);

$cnt = count($pot_matches[1]);
$display = "";
for ($i=0; $i<$cnt; $i++) {
$id_match = array();
// grab the id from the url
preg_match('~([0-9]+)$~', $pot_matches[1][$i], $id_match);
$display .= "Id: " . $id_match[1] . "\n";
$display .= "Pot: " . $pot_matches[2][$i] . "\n";
$display .= "URL: " . $pot_matches[1][$i] . "\n";
$display .= "Price: " . $price_matches[1][$i] . "\n";
$display .= "Change: " . $change_matches[1][$i] . "\n\n";
}

echo $display;

/****
Output:

Id: 5354
Pot:  Plant pot
URL: http://services.runescape.com/m=itemdb_rs/Plant_pot/viewitem.ws?obj=5354
Price: 199
Change: 0

Id: 4440
Pot:  Pot lid
URL: http://services.runescape.com/m=itemdb_rs/Pot_lid/viewitem.ws?obj=4440
Price: 9
Change: 0

*****/
?>

 

Should work as long as it stays constant. You can access the items via array indexes (as they should also stay consistent). May not be the best way to do it, but they do "work". (as I am not a regex expert).

Link to comment
https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1066281
Share on other sites

Works pretty well, except for the price and the change.

 

The problem I'm having is that. The change can be +1.3m, +14.5k, +1000, etc. these can also be negative, or just 0 if there was no change.

 

Also, the price can be 736, 1.4m, 197.5m, 83.7k, etc.

 

I suck with Regex, so I'm having a rather hard time trying to do this.

 

I really appreciate your help though.

Link to comment
https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1066288
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.