防止在空标签之后剥离白色空间



我有一个通过DOMDocument加载的HTML文件,其中我进行了一些DOM操作,用saveHTML

问题是删除输入标签后的白色空间,这是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设置为true,但这仅在关闭标签之前保留空间。

有没有办法使domdocument在我的<input> S?

之后保留白色空间

我知道这是一个旧问题,当我遇到此https://bugs.php.net/bug.php?id=50278

在注释中,它说要通过LIBXML_HTML_NODEFDTD作为这样的选项:

$doc = new DOMDocument();
$doc->loadHTMLFile($file, LIBXML_HTML_NODEFDTD);
echo $doc->saveHTML();

这将保持空白。

最新更新