PHP DOM get nodevalue html? (without stripping tags)
I have never done what you're attempting to do, but as a stab in the dark, using the API docs, does echo $entry->textContent; work?
Adding an update. This is from the comments located on the docs page for DOMNode:
Hi!
Combining all th comments, the easiest way to get inner HTML of the node is to use this function:
<?php function get_inner_html( $node ) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML; } ?>
Or, maybe a simpler method is just to do:
echo $domDocument->saveXML($entry);
Instead of:
echo $entry->nodeValue;
You have to use:
echo $doc->saveXML($entry);
Here is a more complete example that might help others too, $doccontent
is the HTML block as a string:
$doccontent = '<html> …'; // your html string
$dom = new DOMDocument;
$internalErrors = libxml_use_internal_errors(true); // prevent error messages
$content_utf = mb_convert_encoding($doccontent, 'HTML-ENTITIES', 'UTF-8'); // correct parsing of utf-8 chars
$dom->loadHTML($content_utf);
libxml_use_internal_errors($internalErrors); // prevent error messages
$specialdiv = $dom->getElementById('xdiv');
if(isset($specialdiv))
{
echo $dom->saveXML($specialdiv);
}