Page 1 of 1

Variable Nightmares

Posted: Fri Apr 23, 2010 2:55 pm
by jonathantheaker
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:

Code: Select all

<?php

//by RobbieThe1st

//released under Creative Commons Attribution 3.0 United States License.

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

Re: Variable Nightmares

Posted: Fri Apr 23, 2010 3:11 pm
by Christopher
If you look at the code you posted after the like with curl_close($ch), you will see that it is using strpos() to find various strings in the text, calculating the position after the string found, and then using substr() and those positions to extract pieces from the text. See the manual on specifically how these functions work.