我有一个通过 DOMDocument
加载的 html 文件我在其中进行了一些 DOM 操作,输出带有 saveHTML
的 html .
问题是 input 标签后的空格被去掉了,这里是 HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="/style.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
window.jQuery || document.write(unescape('%3cscript src="/script/jquery.min.js"%3e%3c/script%3e'));
</script>
</head>
<body>
<div>
<div>
<form method="post" action="/register/">
<label>First name: <input type="text" name="firstname"></label>
<label>Last name: <input type="text" name="lastname"></label>
<label>Date of birth: <input type="date" name="dateofbirth"></label>
<label>Address: <input type="text" name="address"></label>
<label>Phone number: <input type="text" name="phonenumber"></label>
<label>Sex: <input type="text" name="sex"></label>
<label>Email address: <input type="email" name="email"></label>
<label>Account password: <input type="password" name="password"></label>
<input id="register-button" type="submit" value="Register">
<input type="reset" value="Reset">
<input type="button" value="Cancel">
</form>
</div>
</div>
</body>
</html>
PHP
$template_file = $_SERVER['DOCUMENT_ROOT']."/application/template/template.html";
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadHTMLFile($template_file);
/* dom manipulation, importing and appending nodes from other documents etc */
echo $doc->saveHTML();
它在我尝试的其他标签(<br>
、<hr>
)之后去除了空格,但 <head>
中的标签除外。 .
我尝试设置 formatOutput
为真,但只保留结束标记前的空格。
有没有办法让 DOMDocument 保留我的 <input>
之后的空格?是吗?
最佳答案
我知道这是一个老问题,当我遇到这个 https://bugs.php.net/bug.php?id=50278 时,我正在寻找同样的东西
在注释中它说要传递 LIBXML_HTML_NODEFDTD
作为选项,如下所示:
$doc = new DOMDocument();
$doc->loadHTMLFile($file, LIBXML_HTML_NODEFDTD);
echo $doc->saveHTML();
这将保留空白。
关于php - 防止 DOMDocument 去除空标签后的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114050/