-Karl- Posted June 1, 2010 Share Posted June 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203536-preg_match_all/ Share on other sites More sharing options...
-Karl- Posted June 1, 2010 Author Share Posted June 1, 2010 preg_match_all('#class="([^"]+)">{+,-}([0-9]+).([0-9])#', $content, $out); My poor attempt. Can't figure this out for shit. Quote Link to comment https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1066228 Share on other sites More sharing options...
premiso Posted June 1, 2010 Share Posted June 1, 2010 <<?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). Quote Link to comment https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1066281 Share on other sites More sharing options...
-Karl- Posted June 1, 2010 Author Share Posted June 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1066288 Share on other sites More sharing options...
-Karl- Posted June 3, 2010 Author Share Posted June 3, 2010 Anyone able to help me with this Regex? Quote Link to comment https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1066958 Share on other sites More sharing options...
ZachMEdwards Posted June 5, 2010 Share Posted June 5, 2010 $pattern = '%<td><img src="http://services\.runescape\.com/m=itemdb_rs/\d+_obj_sprite\.gif\?id=(\d+)" alt="(.+)"></td> <td><a href="(http://services\.runescape\.com/m=itemdb_rs/.+/viewitem\.ws\?obj=\d+)"> .+</a></td> <td>(.+)</td>%i'; Don't hate me Quote Link to comment https://forums.phpfreaks.com/topic/203536-preg_match_all/#findComment-1068207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.