Fillie
Fillie

Reputation: 223

How can I remove all <span> tags and their respective content, including other nested elements?

I've tried a few solutions which only remove the the tags themselves leaving the content and any other nested

Regular Expression,

preg_replace('/<span\b[^>]*>(.*?)<\/span>/ig', '', $page->body);

Tried using HTML purifier also,

$purifier->set('Core.HiddenElements', array('span'));

$purifier->set('HTML.ForbiddenElements', array('span')); 

Upvotes: 1

Views: 608

Answers (1)

Niki van Stein
Niki van Stein

Reputation: 10724

Depending on your actual strings and the things you tried you could use a regular expression (assuming your span tags are only span tags). A more "appropriate" solution however would be to use an html parser like DomDocument.

You can use the function document.getElementsByName("span"); to get all the span elements and remove them from the document object.
Then use saveHTML to get the html code back.

You will get something like this:

$doc = new DOMDocument;
$doc->load($yourpage);

$root = $doc->documentElement;

// we retrieve the spans and remove it from the book
$spans = $book->getElementsByTagName('span');
foreach ($spans as $span){
    $root->removeChild($span);
}

echo $doc->saveXML();

Upvotes: 1

Related Questions