Skip to content

Instantly share code, notes, and snippets.

@humbertocastelo
Created January 7, 2018 05:13
Show Gist options
  • Save humbertocastelo/aa028b21d20f04afe909f7e8b2c11e63 to your computer and use it in GitHub Desktop.
Save humbertocastelo/aa028b21d20f04afe909f7e8b2c11e63 to your computer and use it in GitHub Desktop.
Remove Scripts With SRC
<?php
function remove_scripts_with_src( $html ) {
$use_errors = libxml_use_internal_errors( true );
$dom = new \DOMDocument();
$dom->loadHTML( $html );
$scripts = $dom->getElementsByTagName( 'script' );
for ( $i = $scripts->length - 1; $i >= 0; $i-- ) {
if ( !( $item = $scripts->item( $i ) ) ) {
continue;
}
if ( $item->hasAttribute( 'src' ) ) {
$item->parentNode->removeChild( $item );
}
}
$html = $dom->saveHTML();
libxml_clear_errors();
libxml_use_internal_errors( $use_errors );
return $html;
}
$old_html = file_get_contents( 'https://www.facebook.com' );
$new_html = remove_scripts_with_src( $old_html );
print '<pre>' . htmlentities( $new_html ) . '</pre>';
exit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment