Skip to content

Instantly share code, notes, and snippets.

@radiovisual
Created May 20, 2014 11:04
Show Gist options
  • Save radiovisual/f8d04cd6e6da2f7f584b to your computer and use it in GitHub Desktop.
Save radiovisual/f8d04cd6e6da2f7f584b to your computer and use it in GitHub Desktop.
PHP function to strip all html tags and comments from a string
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $document);
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment