xml - DOMXPath/PHP - Get a value only after specific occurrence -
guys i'm parsing url html dom elements.
here code:
<?php $url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822'; libxml_use_internal_errors(true); $dom = new domdocument; $dom->loadhtmlfile($url); $xp = new domxpath($dom); $qry = '//script[starts-with(normalize-space(.), "var colourvariantsinitialdata")]'; $rawtxt = $xp->query($qry)->item(0)->nodevalue; $jsonstart = strpos($rawtxt, '['); $jsonend = strrpos($rawtxt, ']'); $collections = json_decode(substr($rawtxt, $jsonstart, $jsonend - $jsonstart + 1)); foreach ($collections[1]->sizevariants $item) { $sizename = $item->sizename; $priceunformated = $item->prodsizeprices->sellprice; $find = array('£'); $replace = array(''); $price = str_replace($find, $replace, $priceunformated); echo "sizename: <b>$sizename</b> - price: <b>$price</b><br>"; } this code fetching "text" script output source. here complete text script: http://pastebin.com/fwk9z8cp
my code giving following result:
sizename: 7 (41) - price: 27.00 sizename: 8 (42.5) - price: 36.00 sizename: 9 (44) - price: 36.00 sizename: 9.5 (44.5) - price: 36.00 sizename: 11 (46) - price: 36.00 my question is:
how can result specific sizename, example let's sizename 7 (41) ?
thanks in advance!
$specific have string want find. change foreach in code this:
$specific = '7 (41)'; foreach ($collections[1]->sizevariants $item) { $sizename = $item->sizename; if(trim($sizename) == trim($specific)) { $priceunformated = $item->prodsizeprices->sellprice; $find = array('£'); $replace = array(''); $price = str_replace($find, $replace, $priceunformated); echo "sizename: <b>$sizename</b> - price: <b>$price</b><br>"; } }
Comments
Post a Comment