There are two approaches to getting the outer HTML of a DOMDocument node suggested here: How to return outer html of DOMDocument?
I'm interested in why they seem to treat HTML entities differently.
EXAMPLE:
function outerHTML($node) {
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($node, true));
return $doc->saveHTML();
}
$html = '<p>ACME’s 27” Monitor is $200.</p>';
$dom = new DOMDocument();
@$dom->loadHTML($html);
$el = $dom->getElementsByTagname('p')->item(0);
echo $el->ownerDocument->saveHtml($el) . PHP_EOL;
echo outerHTML($el) . PHP_EOL;
OUTPUT:
<p>ACME’s 27” Monitor is $200.</p> <p>ACME’s 27” Monitor is $200.</p>
Both methods use saveHTML()
but for some reason the function preserves html entities in the final output, while directly calling saveHTML()
with a node context does not. Can anyone explain why - preferably with some kind of authoritative reference?