Skip to content

Instantly share code, notes, and snippets.

@mahadazad
Forked from wkjagt/remove_script_tags.php
Created January 11, 2017 08:13
Show Gist options
  • Save mahadazad/0ad6649a2b39cd137bbd15c618674889 to your computer and use it in GitHub Desktop.
Save mahadazad/0ad6649a2b39cd137bbd15c618674889 to your computer and use it in GitHub Desktop.
Remove script tags from html
<?php
function removeDomNodes($html, $xpathString)
{
$dom = new DOMDocument;
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);
while ($node = $xpath->query($xpathString)->item(0)) {
$node->parentNode->removeChild($node);
}
return $dom->saveHTML();
}
// remove comments
removeDomNodes($html, '//comment()');
// remove script tags
removeDomNodes($html, '//script');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment