Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve New Lines when Using the PHP DOMDocument() Class [duplicate]

I have HTML that looks like this when you view the source:

Original HTML

<!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:

New HTML

<!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?

like image 671
GTS Joe Avatar asked Aug 02 '16 05:08

GTS Joe


1 Answers

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

like image 161
Professor Abronsius Avatar answered Nov 01 '22 17:11

Professor Abronsius