Jump to content

extract a string from a bigger string


dzhax

Recommended Posts

I found a script on a forum that i would like to use but 1. i cant get it to work and 2. when i started looking up some of the functions i did not recognize, php.net was quick to inform me that the function ereg() is going to be deprecated.I know this doesn't mean it wont work anymore but, when something is being deprecated that tells me there is something out there that is better.Basically I am making a script that helps you merchant in the game Runescape.It reads a nifty database that gives you live market information and extracts only the current low//med//high prices for that item.from what i am seeing it pulls in the page with cUrl and is trying to extract the numbers after "Minimum price:" "Market price:" and "Maximum price:"example of what the text looks like with current prices: "Minimum price: 1,869 Market price: 1,967 Maximum price: 2,065"im going to post below what i have and maybe someone here can explain what is either going wrong or possibly a better way to write it!Example Url To Use With The Script:http://services.runescape.com/m=itemdb_rs/Mahogany_plank/viewitem.ws?obj=8782

<?//START ADDITIONAL CODING I ADDED//$itemid = $_GET['id'];			 // <- Was a static id$itemname = $_GET['name'];		// <- Was a static name$fetchUrl = "http://services.runescape.com/m=itemdb_rs/" . $itemname . "/viewitem.ws?obj=" . $itemid;//END ADDITIONAL CODING I ADDED//$curl = curl_init();curl_setopt($curl, CURLOPT_URL, "http://services.runescape.com/m=itemdb_rs/" . $itemname . "/viewitem.ws?obj=" . $itemid);curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);curl_setopt($curl, CURLOPT_TIMEOUT, 6);$fullge =  curl_exec($curl);$curl_http = curl_getinfo($curl, CURLINFO_HTTP_CODE);curl_close($curl);if ($curl_http == "404") {	// For some reason Jagex has deleted the GE :D	// Do whatever you need to here.}if (strpos($fullge, "The item you were trying to view could not be found.") !== FALSE) {	// The item doesn't exist.	// Do whatever you need to here.}ereg('Minimum price: ([0-9.km,]+)', $fullge, $regex['minprice']);ereg('Market price: ([0-9.km,]+)', $fullge, $regex['markprice']);ereg('Maximum price: ([0-9.km,]+)', $fullge, $regex['maxprice']);/* * Element [1] of the $regex arrays now contains the prices from the GE. * Running them through trim() first is a good idea just to be safe. * */// Just to prove it works!print_r($regex);//START ADDITIONAL CODING I ADDED//echo '<br />';echo $fetchUrl;echo '<br />';echo 'Min Price: ' . $regex['minprice'] . '<br />';echo 'Med Price: ' . $regex['marketprice'] . '<br />';echo 'Max Price: ' . $regex['maxprice'];//print($fullge);//END ADDITIONAL CODING I ADDED//?>

Link to comment
Share on other sites

The point is, EREG (extended regular expressions) have been deprecated in favour of PCRE (Perl-compatible regular expressions) in PHP.

Link to comment
Share on other sites

i got the info to display now, but not exactly how i want it to.for me to find the correct information i must know the line of which the it will be on. in this case it is currently on line 238 of the source.i have been reading about the the preg_grep function and i think this is what i need to "automate" finding the line of which my number is on.an example URL would be .../index.php?obj=8782what i have so far:

<?$itemid = $_GET['obj'];$fetchUrl = "http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=" . $itemid;$curl = curl_init();curl_setopt($curl, CURLOPT_URL, "http://services.runescape.com/m=itemdb_rs/" . $itemname . "/viewitem.ws?obj=" . $itemid);curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);curl_setopt($curl, CURLOPT_TIMEOUT, 6);$fullge =  curl_exec($curl);$lines = explode("\n", $fullge);//$minPLine = preg_grep("Minimum Price: ",$lines);$minP = preg_split('/ /', $lines[238], -1, PREG_SPLIT_OFFSET_CAPTURE);echo $minP[2][0];?>

I am trying to use the $minPLine = preg_grep("Minimum Price: ",$lines); to replace the $lines[238] with $minPLine in $minP = preg_split('/ /', $lines[238], -1, PREG_SPLIT_OFFSET_CAPTURE);but im getting: Warning: preg_grep() [function.preg-grep]: Delimiter must not be alphanumeric or backslash inEDIT:Yay I figured it out!!! :) ftw.

<?$itemid = $_GET['obj'];$priceKind = "";$curl = curl_init();curl_setopt($curl, CURLOPT_URL, "http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=" . $itemid);curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);curl_setopt($curl, CURLOPT_TIMEOUT, 6);$fullge =  curl_exec($curl);$lines = explode("\n", $fullge);$minPLine = preg_grep("/Minimum price:<\/b> /", $lines);if (!$minPLine) die("I Fail");foreach ($minPLine as $output_this){$minPNum = $output_this;}$minP = preg_split('/ /', $minPNum, -1, PREG_SPLIT_OFFSET_CAPTURE);echo $minP[2][0];?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...