GTS*_*Joe 7 php newline domdocument
当您查看源代码时,我的 HTML 如下所示:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是在我这样做之后:
$dom = new DOMDocument();
$dom->loadHTML($html);
$dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)
我的源代码变成了这样:
<!DOCTYPE html><html><head></head><body></body></html>
Run Code Online (Sandbox Code Playgroud)
使用 PHP DOMDocument() 类及其方法时如何保留新行和空格?
要保留空白,请尝试以下操作:
$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;
Run Code Online (Sandbox Code Playgroud)
您还可以设置一些其他属性 - 您可以在手册中找到它们