我已将内容从博客帐户导入到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(' ', ' ', $content);
$content = "<p>".implode("</p>\n\n<p>", preg_split('/\n(?:\s*\n)+/', $content))."</p>";
return $content;
由于某种原因,尽管我的页面中打印了随机的DOCTYPE,但我不知道为什么。
<p>!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” “http://www.w3.org/TR/REC-html40/loose.dtd”>
<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/