SRL Archive - Web Development

More php help


smurg

2011-08-12 23:04:59

Grrrr. I'm trying to extract ID's off runescsape GE database but keep getting mumbo jumbo.

Last thread: http://villavu.com/forum/showthread.php?t=65611

Here was the main code:
[php]
<?php
function str_between($start, $end, $string){
$string = " ".$string;
$ini = strpos($string, $start);
if ($ini == 0) {
return "";
}
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}

function contains($string, $substring) {
$pos = strpos($string, $substring);
if($pos === false) {
return false;
}
else {
return true;
}
}

function parse_table($html) {
preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html);
preg_match_all("/<th.*?>(.*?)<\/[\s]*th>/", $table_html[0], $matches);
$row_headers = $matches[1];
preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);
$table = array();
foreach($matches[1] as $row_html) {
preg_match_all("/<td.*?>(.*?)<\/[\s]*td>/", $row_html, $td_matches);
$row = array();
for($i=0; $i<count($td_matches[1]); $i++) {
$td = strip_tags(html_entity_decode($td_matches[1][$i]));
if(contains($td, "Tarromin pot")) {
$price = strip_tags(html_entity_decode($td_matches[1][$i + 1]));
$movement = strip_tags(html_entity_decode($td_matches[1][$i + 2]));
return array($price, $movement);
}
$row[$row_headers[$i]] = $td;
}
if(count($row) > 0)
$table[] = $row;
}
return false;
}

$ch = curl_init("http://services.runescape.com/m=itemdb_rs/results.ws?query=tarromin+potion&price=all&members=");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
$contents = str_between('<td class="navright">', '<div class="results_nav">', $contents);
print_r(parse_table($contents));
?>
[/php]And I'm using, atm:

[php]
$scraped_id = str_between('<td><img src="http://services.runescape.com/m=itemdb_rs/3450_obj_sprite.gif?id=','" alt="', $contents);
[/php]To get the Id, but if the item isn't the first row, then it fails. I tried adding in the item name during and it doesnt work:

[php]
$scraped_id = str_between('<td><img src="http://services.runescape.com/m=itemdb_rs/3450_obj_sprite.gif?id=','" alt="Leather', $contents);
[/php]It grabs abunch of the other html and bs because of the extra " I think. Anyone have any ideas?

smurg

2011-08-12 23:12:29

HERWDTYJRERGO<SEGOERMGOWERMFWOFMQOWEFMOWEMFDWEdq

I figured it out. SFIOSJMDFINSGNWIEGNWEg

It grabs the first:

[php]'<td><img src="http://services.runescape.com/m=itemdb_rs/3450_obj_sprite.gif?id='[/php]from the first row, and then the second part from the second row and everything in between is bs. Hmmm brainstorm time.

WOOOOOOT:

$page_data is my $content
[php]
$item_name_edited = str_replace(" ", "_", $item_name);
echo "Test: ".str_between('/'.$item_name_edited.'/viewitem.ws?obj=','"> '.$item_name.'</a></td>', $page_data);
[/php]