Skip to content

Instantly share code, notes, and snippets.

@wkjagt
Last active November 12, 2020 06:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wkjagt/5599321 to your computer and use it in GitHub Desktop.
Save wkjagt/5599321 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');
@AcTiv3MineD
Copy link

Very useful, thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment