note 65180 modified in function.strip-tags by bjori

From: Date: Wed, 26 Apr 2006 19:30:14 +0000
Subject: note 65180 modified in function.strip-tags by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-109780@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. --was-- 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. http://php.net/manual/en/function.strip-tags.php

« previous php.notes (#109780) next »