note 65180 added to function.strip-tags

From: Date: Wed, 26 Apr 2006 06:50:58 +0000
Subject: note 65180 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-109722@lists.php.net to get a copy of this message
This function does not work correctly in all situations. Let's say you have an image tag, except in this particular case, there's a greater-than sign in the alt attribute. Example: <html> <img src="next.jpg" alt="Next >" /> </html> The function strip_tags() will think that this tag ends just before the last quotation-mark, when instead it ends a few characters later. This can be solved using regular expressions: <?php $regex ='/</?\w+((\s+\w+(\s*=\s*'; $regex .= '(?:".*?"|\\'.*?\\'|[^\\'">\s]+))?)+'; $regex .= '\s*|\s*)/?>/i'; $text = preg_replace($regex, '', $text); ?> If you'd like to simply strip particular tags (instead of all tags) like this, replace the first \\w with the tag name (e.g. img). However, this method would not handle any embedded scripting very well (<script>, <style>, <?php ?>, etc.), as it would just strip the tags but not the inner content. I apologize for breaking up the regular expression on three lines, but it would not let me post my comment using such a long regular expression on one line. You may or may not prefer to keep it broken up in your code. ---- Server IP: 216.194.113.175 Probable Submitter: 70.226.186.165 ---- X-Spam-Status: Yes, hits=5.7 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_40_50,HTML_IMAGE_ONLY_06,HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.strip-tags.php Edit -- http://master.php.net/note/edit/65180 Del: integrated -- http://master.php.net/note/delete/65180/integrated Del: useless -- http://master.php.net/note/delete/65180/useless Del: bad code -- http://master.php.net/note/delete/65180/bad+code Del: spam -- http://master.php.net/note/delete/65180/spam Del: non-english -- http://master.php.net/note/delete/65180/non-english Del: in docs -- http://master.php.net/note/delete/65180/in+docs Del: other reasons-- http://master.php.net/note/delete/65180 Reject -- http://master.php.net/note/reject/65180 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#109722) next »