The following snippet of PHP code creates $desc alright, but I like it to introduce two (2) blank spaces between every dpItemFeatureList found as it goes through its iteration.
I can’t seem to garner exactly what or where to add a snippet to do this?
function get_description($asin){
$url = 'http://www.amazon.com/gp/aw/d/' . $asin . '?d=f&pd=1';
$data = request_data($url);
$desc = '';
if ($data) {
$dom = new DOMDocument();
@$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
if (preg_match('#dpItemFeaturesList#',$data)){
$k = $xpath->query('//ul[@class="dpItemFeaturesList"]');
foreach ($k as $c => $tot) {
$desc .= $tot->nodeValue;
}
}
}
return $desc;
Looking at the code you have shared here and consequently having a look at the data that you are processing (a sample of which I have pasted here) you actually want to collect the text within the
<li>child elements of the<ul class="dpItemFeaturesList">node.In your original code snippet your XPath is as follows:
This will only select the
<ul>element and not the child elements. Consequently when you try to do a$tot->nodeValueit will concatenate all the text within all it’s child nodes without spaces (ah ha, the real reason why you want spaces in the first place).To fix this we should do two things:
<li>nodes within the appropriate node. Change the XPath to//ul[@class="dpItemFeaturesList"]/li.foreachloop concatenate 2 non-breakable spaces (because this is HTML) to the$descvariable.Here
$cis the array index.We check for
$c > 0so that you will not get extra spaces after the last node in the loop.P.S.: Unrelated to your original question. The code for which you shared a link has an undefined variable
$timestampin$date = date("format", $timestamp);online 116.