note 96371 deleted from function.strip-tags by danbrown
| From: | danbrown@php.net | Date: | Tue, 23 Feb 2010 18:23:57 +0000 |
| Subject: | note 96371 deleted from function.strip-tags by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166295@lists.php.net to get a copy of this message | ||
Note Submitter: kyperkins at shadowdev dot com
----
this is a modified script from admin at automapit dot com back in 2006. I've moved the HTML
stripper to the bottom so it will strip out all tags in general (before, it would strip the HTML
tags, then skip over the CSS).
This way, it processes the code and strips out tags in this order: Javascript, CSS (style),
Comments, all other HTML tags.
<?php
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<style[^>]*?>.*?</style>@siU', // Strip style tags
properly
'@<![\s\S]*?--[ \t\n\r]*>@', // Strip multi-line comments
including CDATA
'@<[\/\!]*?[^<>]*?>@si' // Strip out HTML tags
);
$text = preg_replace($search, '', $document);
return $text;
}
?>
all credits go to admin at automapit dot com. just thought I'd point this out. Aside from that,
it works beautifully.
Keep in mind: If you want to keep the contents of <select> and <option> tags, change the
<style> line to "@si" (aka: take out the "U"). Otherwise, these will be
treated like HTML tags and be completely taken out.