note 74316 added to function.strip-tags

From: Date: Wed, 04 Apr 2007 23:24:14 +0000
Subject: note 74316 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-125100@lists.php.net to get a copy of this message
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 ---- Server IP: 64.71.164.2 Probable Submitter: 71.128.91.6 ---- Manual Page -- http://www.php.net/manual/en/function.strip-tags.php Edit -- https://master.php.net/note/edit/74316 Del: integrated -- https://master.php.net/note/delete/74316/integrated Del: useless -- https://master.php.net/note/delete/74316/useless Del: bad code -- https://master.php.net/note/delete/74316/bad+code Del: spam -- https://master.php.net/note/delete/74316/spam Del: non-english -- https://master.php.net/note/delete/74316/non-english Del: in docs -- https://master.php.net/note/delete/74316/in+docs Del: other reasons-- https://master.php.net/note/delete/74316 Reject -- https://master.php.net/note/reject/74316 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125100) next »