regex - 为什么在我的页面上打印Doctype?

标签 regex wordpress xpath blogger

我已将内容从博客帐户导入到Wordpress博客中。

我不得不应用一些xpath和regex来删除一些讨厌的格式。

global $post;
$html = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$doc = new DOMDocument();@$doc - > loadHTML($html);
$xpath = new DOMXPath($doc);
foreach($xpath - > query('//br[not(preceding::text())]') as $node) {
    $node - > parentNode - > removeChild($node);
}
$nodes = $xpath - > query('//a[string-length(.) = 0]');
foreach($nodes as $node) {
    $node - > parentNode - > removeChild($node);
}
$nodes = $xpath - > query('//*[not(text() or node() or self::br)]');
foreach($nodes as $node) {
    $node - > parentNode - > removeChild($node);
}
remove_filter('the_content', 'wpautop');
$content = $doc - > saveHTML();
$content = ltrim($content, '<br>');
$content = strip_tags($content, '<br> <a> <iframe>');
$content = preg_replace(array('/(<br\s*\/?>\s*){1,}/'), array('<br/><br/>'), $content);
$content = str_replace('&nbsp;', ' ', $content);
$content = "<p>".implode("</p>\n\n<p>", preg_split('/\n(?:\s*\n)+/', $content))."</p>";
return $content;


由于某种原因,尽管我的页面中打印了随机的DOCTYPE,但我不知道为什么。

<p>!DOCTYPE html PUBLIC &#8220;-//W3C//DTD HTML 4.0 Transitional//EN&#8221; &#8220;http://www.w3.org/TR/REC-html40/loose.dtd&#8221;>
    <br/>
    <br/>When the battle is on between contestants in a talent show, it gets really competitive when down to the last four. X-FactorUSAcontestant Marcus Canty knows this all too well as this is the stage he was voted off of the show earlier this year.
    <br/>
    <br/>
</p>


有人可以向我指出为什么会这样吗?

最佳答案

当您使用DOMDocument加载一段html代码时,会自动将Doctype,html,head和body标签(如果缺少)添加到这部分html(并关闭未关闭的标签),以使其成为“有效”的html文档。因此,当您使用saveHTML时,请保存所有这些内容。如果我还记得的话,可以在PHP手册中找到一些避免这种情况的技巧(在帖子中)

关于regex - 为什么在我的页面上打印Doctype?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21262385/

相关文章:

php - 更新 PHP5.4 和 MySQL4.1+ 时 Wordpress 连接错误

php - 使用 xpath 选择第一个结果的 child

regex - Perl 5.018 中的自定义字符类

javascript - wordpress pexeto 网格库自定义链接在单击时不会更改,在刷新时会更改

python - Pandas 用正则表达式将列表分成几列

php - 删除布鲁克林主题的视差背景

php - 如何防止文档类型被添加到 HTML 中?

xml - Powershell选择XML节点,其中属性包含在数组中

mysql - ER_TRUNCATED_WRONG_VALUE_FOR_FIELD 将一些字符串保存到 mysql

java - 扫描数据并将其添加到具有未指定分隔符的数组