Reputation: 907
I have to fetch the value $1999.00 from the following code snippet...
<span class="mainprodpricez" itemprop="price">$1999.00
<span style="font-size:0.4em;display:block;"> including $100 cashback from Shopper</span>
<span style="font-size:0.4em;display:block;font-weight:bold;color:#cc0000;float:left;">You pay $2249.00 today. </span>
Details and claim form.</a></span>
To do this I have done the following coding:
foreach($dom->getElementsByTagName('span') as $p) {
if($p->getAttribute('class') == 'mainprodpricez') {
$name = $p->nodeValue;
}
}
This gives me the following result:
Â$1999.00 including ÂÂ$100 cashback from Shopper You pay Â$2249.00 today. Details and claim form
Actually its fetching the whole nodeValue of the span class mainprodpricez...can anyone please help me on this.
Upvotes: 2
Views: 61
Reputation: 316969
The nodeValue
attribute gives you the combined text of all the child nodes. You want to get the text value of the first child text node only.
Change your code to
$name = $p->firstChild->nodeValue;
You can also do it with XPath:
$xpath = new DOMXPath($dom);
$query = '//span[@class="mainprodpricez"]/text()[1]';
foreach ($xpath->evaluate($query) as $price) {
echo $price->nodeValue;
}
Also see my explanation in DOMDocument in php for a general overview of the node concept.
Upvotes: 1