jonathan.Theaker Posted April 23, 2010 Share Posted April 23, 2010 Hello, I was given this piece of code, it basically scrapes data from another website then prints the requested data on the loaded page, unfortunately I need to be able to pick out exactly which bits of the data I want printing out instead of all of it. I've been told I can achieve this by storing the scraped data in variables then calling them up... but I have no idea what that means and how I would go about implementing this. could anyone help me achieve this? Here is the code: <?php //by RobbieThe1st function ge_info($id) { $ctime = microtime(true); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,'http://itemdb-rs.runescape.com/viewitem.ws?obj='.$id); curl_setopt($ch,CURLOPT_TIMEOUT,10); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12'); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $file = curl_exec($ch); curl_close($ch); $out = array(); $v000 = strpos($file,'<div class="subsectionHeader">',0) + 30; $v00 = strpos($file,'<div class="subsectionHeader">',$v000) + 30; $v0 = strpos($file,'<div class="subsectionHeader">',$v00) + 31; $v1 = strpos($file,'</div>',$v0) + 6; $out['name'] = trim(substr($file,$v0,($v1 - 6) - $v0)); if($out['name'] == 'Error</div>') { return false; }; $code = explode(chr(10),substr($file,$v1,strpos($file,'</div>',$v1) - $v1)); $out['examine'] = trim($code[3]); $find = array('Minimum price:','Market price:','Maximum price:','7 Days:','30 Days:'); $names = array('low_price','market_price','max_price','7d_change','30d_change' ); $fN = 0; for($i=0, $count = count($code); $i<$count; ++$i) { if($v1 = strpos($code[$i],$find[$fN])) { $out[$names[$fN]] = str_replace(array(',','+','%'),false, trim(strip_tags(substr($code[$i],$v1 + strlen($find[$fN]))))); $fN ++; if($fN == 5) { break; }; }; }; return $out; }; print_r(ge_info(2)); ?> I couldn't thank anyone enough that can offer me some insight or an example. Regards Jonathan Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/ Share on other sites More sharing options...
mikesta707 Posted April 23, 2010 Share Posted April 23, 2010 exactly what information do you want to print out. Since no one here made this script, it will be hard to tell you what to store into variables. what information are you looking for? where on the page is it? Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047368 Share on other sites More sharing options...
jonathan.Theaker Posted April 23, 2010 Author Share Posted April 23, 2010 you can find this script running at http://hintscape.net/x.php i would like to be able to display just one of the following "Dragon 2h sword" or "A two-handed Dragon Sword" or "1.5m" "1.6m" or "1.7m" . Basicly display the outcome of [name], [examine], [low_price], [market_price] & [max_price] but each needs to be able to be displayed on its own with out the others so i can enter each outcome in to a table like this one http://www.tip.it/runescape/index.php?rs2item_id=4568 Thanks for the reply Regards Jonathan Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047381 Share on other sites More sharing options...
mattal999 Posted April 23, 2010 Share Posted April 23, 2010 Replace the: print_r(ge_info(2)); at the bottom with: echo "Item name: ".ge_info(2)['name']; If you can understand the basics of using echo() then you should be on your way. Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047394 Share on other sites More sharing options...
jonathan.Theaker Posted April 23, 2010 Author Share Posted April 23, 2010 Replace the: print_r(ge_info(2)); at the bottom with: echo "Item name: ".ge_info(2)['name']; If you can understand the basics of using echo() then you should be on your way. Ok so i have read a tutorial on how to create strings and echo them, ive had a play and i keep getting errors. if i try your exact example above i get the following error: Parse error: syntax error, unexpected '[', expecting ',' or ';' in /homepages/8/d321609305/htdocs/hintscape.net/x.php on line 82 Again am I just being a numb skull or incredibly stupid... thanks for persevering! Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047415 Share on other sites More sharing options...
Baez Posted April 23, 2010 Share Posted April 23, 2010 It's telling you the error right there. Look at line 82 and see if you can see anything wrong. If not post that line here. Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047418 Share on other sites More sharing options...
jonathan.Theaker Posted April 23, 2010 Author Share Posted April 23, 2010 Sorry, i saw that it was telling me i just couldn't understand what the error was as everything looked right to me, but i am a PHP newbie. The error line is: echo "Item name: ".ge_info(2)['name']; Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047420 Share on other sites More sharing options...
Baez Posted April 23, 2010 Share Posted April 23, 2010 Try $out = ge_info(2); echo "Item name: " . $out['name']; Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047421 Share on other sites More sharing options...
mrMarcus Posted April 23, 2010 Share Posted April 23, 2010 ya, this: ge_info(2)['name'] isn't going to work. Advice: since you're a newbie, it's no better of a time to start practicing good coding styles/formatting, especially when seeking help/advice. what i mean is, it is very hard to read code where every line starts at the first character space. make sure and use indenting in your code. Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047422 Share on other sites More sharing options...
jonathan.Theaker Posted April 23, 2010 Author Share Posted April 23, 2010 Thats brilliant! Its perfect for what i need it for, ive actualy just bought PHp for dummies off of ebay as i think it can bring so much to my site. Thanks for all the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047432 Share on other sites More sharing options...
jonathan.Theaker Posted April 23, 2010 Author Share Posted April 23, 2010 Oh and here is the cleaner indented code encase it is usefull to anyone else. <?php //by RobbieThe1st function ge_info($id) { $ctime = microtime(true); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,'http://itemdb-rs.runescape.com/viewitem.ws?obj='.$id); curl_setopt($ch,CURLOPT_TIMEOUT,10); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12'); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $file = curl_exec($ch); curl_close($ch); $out = array(); $v000 = strpos($file,'<div class="subsectionHeader">',0) + 30; $v00 = strpos($file,'<div class="subsectionHeader">',$v000) + 30; $v0 = strpos($file,'<div class="subsectionHeader">',$v00) + 31; $v1 = strpos($file,'</div>',$v0) + 6; $out['name'] = trim(substr($file,$v0,($v1 - 6) - $v0)); if($out['name'] == 'Error</div>') { return false; } ; $code = explode(chr(10),substr($file,$v1,strpos($file,'</div>',$v1) - $v1)); $out['examine'] = trim($code[3]); $find = array('Minimum price:','Market price:','Maximum price:','7 Days:','30 Days:'); $names = array('low_price','market_price','max_price','7d_change','30d_change'); $fN = 0; for($i=0, $count = count($code); $i<$count; ++$i) { if($v1 = strpos($code[$i],$find[$fN])) { $out[$names[$fN]] = str_replace(array(',','+','%'),false, trim(strip_tags(substr($code[$i],$v1 + strlen($find[$fN]))))); $fN ++; if($fN == 5) { break; } ; } ; } ; return $out; } ; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047433 Share on other sites More sharing options...
Baez Posted April 24, 2010 Share Posted April 24, 2010 My advice on books are these two: PHP and MySQL Web Development - Fourth Edition Wicked Cool PHP PHP for dummies isn't very intuitive and leaves beginner programmers stranded or confused a lot of the time. If you pick up the two I suggested you'll be on your way in no time Quote Link to comment https://forums.phpfreaks.com/topic/199541-my-variables-nightmare/#findComment-1047435 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.