How to prevent the extension of DOMXPath HTML objects?

I use DOMDocument and DOMXPath in PHP to search for elements in an HTML document. This document contains HTML objects such as & nbsp; and I would like these objects to be saved on XPath output.

$doc = new DOMDocument();
$doc->loadHTML('<html><head></head><body>&nbsp;Test</body></html>');

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//body');

foreach($nodes as $node) {
    echo $node->textContent;
}

This code produces the following output (UTF-8):

[space]Test

But I would like to have this:

&nbsp;Test

It may have something to do with LibXML, which uses PHP internally, but I could not find any function that stores HTML objects.

Do you have an idea?

+5
source share
2 answers

XPath XML-, . - XML-, , , , &nbsp; §nbsp;.

+4

XPath , &nbsp; &#xA0;' -- the character is always provided to it as a character entity -- & # 160`.

+2

All Articles