我只想知道这些之间的区别:
$post_content_html = $dom->saveHTML($dom->documentElement);
并且
$post_content_html = $dom->saveHTML();
我的测试表明没有任何不同。但是我应该何时将$dom->documentElement
传递给saveHTML()
函数?
我的完整代码删除了除特定属性之外的所有属性:
// to make tags stable, wrap them into <html> tag
$post_content_html = "<html>".$post_content_html."</html>"
$dom = new DOMDocument;
$dom->loadHTML(mb_convert_encoding($post_content_html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//@*');
foreach ($nodes as $node) {
if($node->nodeName != "src" && $node->nodeName != "href" && $node->nodeName != "alt") {
$node->parentNode->removeAttribute($node->nodeName);
}
}
// maybe I have to pass $dom->documentElement to the saveHTML() function
$post_content_html = $dom->saveHTML();
// string <html> tag which been wrapped
$post_content_html = preg_replace('/^<html>/', '', $post_content_html);
$post_content_html = preg_replace('/<\/html>$/', '', $post_content_html);
答案 0 :(得分:3)