Am I doing something wrong here? I am trying to remove all the tags that are of one of the following: style, script, pre, code
.
When I do a dump on the end result, the items that are in that block are still there.
$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);
Your code, in isolation, works fine. What is likely happening is you're working within a namespace and so your instanceof
checks should use the fully-qualified names \DOMNodeList
and \DOMElement
(note the leading backslashes.)
Maybe this solution is interesting for you:
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);