我在这里做错什么了吗?我正在尝试删除所有的标签,其中之一:style, script, pre, code。
当我对最终结果进行转储时,该块中的项仍然存在。
$removes = $xpath->query('//style | //script | //pre | //code');
if($removes instanceof DOMNodeList){
foreach($removes as $removable){
if($removable instanceof DOMElement){
$removable->parentNode->removeChild($removable);
}
}
}
$content = $this->document->getElementsByTagName('body')->item(0)->nodeValue;
var_dump($content);发布于 2015-10-08 21:33:54
孤立地说,您的代码运行良好。可能发生的情况是您在名称空间中工作,因此您的instanceof检查应该使用完全限定名\DOMNodeList和\DOMElement (注意前面的反斜杠)。
发布于 2015-10-08 20:54:00
也许这个解决方案对你来说很有趣:
function strip_selected_tags($text, $tags = array())
{
foreach ($tags as $tag){
if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
$text = str_replace($found[0],$found[1],$text);
}
}
return $text;
}
$tags = array( 'style', 'script', 'pre', 'code');
echo strip_selected_tags($text,$tags);https://stackoverflow.com/questions/33025131
复制相似问题