note 74316 deleted from function.strip-tags by nlopess
| From: | nlopess@php.net | Date: | Thu, 05 Apr 2007 19:38:01 +0000 |
| Subject: | note 74316 deleted from function.strip-tags by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125115@lists.php.net to get a copy of this message | ||
Note Submitter: Bryan Garaventa
----
I've been trying to create an RSS feed script (
http://gutterstar.net/rss.php
) that could not only strip embedded HTML, but also be able to understand when a CDATA tag was
present within the item description. There were a lot of great suggestions to start with from this
page, however, the CDATA tag was always stripped with all other HTML, so nothing worked correctly.
Well, til now anyway. Here is the code that makes it work. This code is based on suggested solutions
from this page...
function html2txt($document) {
$tmp = strtolower($document);
$isCdata = strpos($tmp, '<![cdata[');
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<style[^>]*?>.*?</style>@siU', // Strip style tags
properly
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
including CDATA
);
if ($isCdata === false) {
$text = preg_replace($search, '', $document);
} else {
$arr0 = explode('<![', $document);
$fir = $arr0[0];
$arr1 = explode('[', $arr0[1]);
$mid0 = explode(']]>', $arr1[1]);
$mid = $mid0[0];
$las = $mid0[1];
$text = preg_replace($search, '', $fir);
$text .= $mid;
$text .= preg_replace($search, '', $las);
}
return $text;
}
This works great for properly stripping RSS feed content.
Best wishes,
Bryan Garaventa
http://gutterstar.net