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> 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:
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?
source
share