If I use saveHTML()
without the optional DOMnode
parameter it works as expected:
$html = '<html><body><div>123</div><div>456</div></body></html>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = true;
$dom->formatOutput = false;
$dom->loadHTML($html, LIBXML_HTML_NODEFDTD);
echo $dom->saveHTML();
<html><body><div>123</div><div>456</div></body></html>
But when I add a DOMNode
parameter to output a subset of the document it seems to ignore the formatOutput
property and adds a bunch of unwanted whitespace:
$body = $dom->getElementsByTagName('body')->item(0);
echo $dom->saveHTML($body);
<body> <div>123</div> <div>456</div> </body>
What gives? Is this a bug? Is there a workaround?
Is this a bug?
Yes, it's a bug and it's reported here
Is there a workaround?
Stick with Nigel's solution for now
Did they fix it?
Yes, as of 7.3.0 alpha3 this is a fixed bug
Check it here
If you know your document is going to be valid XML as well, you can use saveXML()
instead...
$html = '<html><body><div>123</div><div>456</div></body></html>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = true;
$dom->formatOutput = false;
$dom->loadHTML($html, LIBXML_HTML_NODEFDTD);
$body = $dom->getElementsByTagName('body')->item(0);
echo $dom->saveXML($body);
which gives...
<body><div>123</div><div>456</div></body>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With