How to clean certain HTML tags with PHP

If you have a string which has HTML and you want to remove only certain tags from it, use the snippet below.

DOMDocument

PHP has this DOMDocument class available for us https://secure.php.net/manual/en/class.domdocument.php

function removeTag($content, $tagName) {
    $dom = new DOMDocument();
    $dom->loadHTML('<?xml encoding="utf-8" ?>' . $content);

    $nodes = $dom->getElementsByTagName($tagName);
    while ($node = $nodes->item(0)) {
        $replacement = $dom->createDocumentFragment();
        while ($inner = $node->childNodes->item(0)) {
            $replacement->appendChild($inner);
        }
        $node->parentNode->replaceChild($replacement, $node);
    }

    # remove <!DOCTYPE
    $dom->removeChild($dom->doctype);

    $nodes = $dom->getElementsByTagName('html');
    while ($node = $nodes->item(0)) {
        $replacement = $dom->createDocumentFragment();
        while ($inner = $node->childNodes->item(0)) {
            $replacement->appendChild($inner);
        }
        $node->parentNode->replaceChild($replacement, $node);
    }

    $nodes = $dom->getElementsByTagName('body');
    while ($node = $nodes->item(0)) {
        $replacement = $dom->createDocumentFragment();
        while ($inner = $node->childNodes->item(0)) {
            $replacement->appendChild($inner);
        }
        $node->parentNode->replaceChild($replacement, $node);
    }

    return str_replace('<?xml encoding="utf-8" ?>', '', $dom->saveHTML());
}

$content = '<span>This <b>is</b> an <span>example</span></span>';

echo removeTag($content, 'span'); // "This <b>is</b> an example"Code language: PHP (php)

Leave a Reply

Your email address will not be published. Required fields are marked *