note 65180 deleted from function.strip-tags by betz

From: Date: Wed, 24 May 2006 11:08:05 +0000
Subject: note 65180 deleted from function.strip-tags by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111932@lists.php.net to get a copy of this message
Note Submitter: soapergem at gmail dot com ---- 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.

« previous php.notes (#111932) next »