2
votes

I have the following html code

    <div id="brandModel">
        Jeep Grand Cherokee,
        <span class="red">529.80&nbsp;EUR</span>
    </div>

The following PHP/Xpath works, but also returns the text inside span.

$xPathcarName = $xpath->query( "//div[@id='brandModel']" ); echo $xPathcarName->item(0)->textContent\r\n";

Output: Jeep Grand Cherokee, 529.80 EUR

I only want the textContent inside the div, not inside the span. Is this possible?

Thanks

1

1 Answers

1
votes

Perfectly possible.

$xPathcarName = $xpath->query( "//div[@id='brandModel']" );
echo $xPathcarName->item(0)->firstChild->textContent . "\r\n";

Remember that in a DOMDocument loaded as XML/XHTML, text is actually a DOMText node. You can see this by looping through the children of your div.