$tags = array(
"applet" => 1,
"script" => 1
);
$html = file_get_contents("test.html");
$dom = new DOMdocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$body = $xpath->query("//body")->item(0);
我是在循环浏览网页的“正文”并删除$ tags数组中列出的所有不需要的标记,但我找不到办法。那我怎么能这样做呢?
答案 0 :(得分:6)
您是否考虑过HTML Purifier?从你自己的html清理开始只是重新发明轮子,并不容易实现。
此外,黑名单方法也很糟糕,请参阅SO/why-use-a-whitelist-for-html-sanitizing
您可能还有兴趣阅读how to cinfigure allowed tags & attributes或testing HTML Purifier demo
答案 1 :(得分:5)
$tags = array(
"applet" => 1,
"script" => 1
);
$html = file_get_contents("test.html");
$dom = new DOMdocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
for($i=0; $i<count($tags); ++$i) {
$list = $xpath->query("//".$tags[$i]);
for($j=0; $j<$list->length; ++$j) {
$node = $list->item($j);
if ($node == null) continue;
$node->parentNode->removeChild($node);
}
}
$string = $dom->saveXML();
类似的东西。