Skip to content

Instantly share code, notes, and snippets.

@villesiltala
Last active August 16, 2018 14:20
Show Gist options
  • Save villesiltala/0c7b655f0fd553f6874daddf39539d18 to your computer and use it in GitHub Desktop.
Save villesiltala/0c7b655f0fd553f6874daddf39539d18 to your computer and use it in GitHub Desktop.
PHP - Strip HTML tags and content from a string
<?php
/**
* A script for removing a set of HTML tags and their content.
* This example strips all style tags and CSS inside them.
* $content is assumed to contain some HTML.
* To extend this, change or add tags to the $unwanted array.
*/
$unwanted = [
'style',
];
foreach ( $unwanted as $tag ) {
$content = preg_replace( "/(<$tag>.*?<\/$tag>)/is", '', $content );
}
unset( $tag );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment