I have HTML that looks like this when you view the source:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
But after I do:
$dom = new DOMDocument();
$dom->loadHTML($html);
$dom->saveHTML();
My source code turns to this:
<!DOCTYPE html><html><head></head><body></body></html>
How can I preserve new lines and white space when using the PHP DOMDocument() class and its methods?
To preserve the whitespace, try something along these lines:
$dom=new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput=false;
$dom->preserveWhiteSpace=true;
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->strictErrorChecking=false;
$dom->recover=true;
There are a few other propertes you can set - you'll find them in the manual
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