Last Updated: February 25, 2016
· wkjagt

Remove anything from html

Removing script tags or comments from html using regular expressions is a bad idea so better use PHP's DOMDocument.

function removeDomNodes($html, $xpathString)
    $dom = new DOMDocument;

    $xpath = new DOMXPath($dom);
    while ($node = $xpath->query($xpathString)->item(0))
    return $dom->saveHTML();

For example, to remove all comments from an HTML string, pass the xpath for comments:

$html = removeDomNodes($html, '//comment()');

Or to remove all script tags:

$html = removeDomNodes($html, '//script');