note 55427 added to function.strip-tags

From: Date: Wed, 03 Aug 2005 10:24:47 +0000
Subject: note 55427 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-92999@lists.php.net to get a copy of this message
I created a nice function to strip ALL html-tags from a string. It uses a regular expression to remove a complete tag (incl. any attributes within that tag). Note: I had to split up the $expr-value for this post. You can use it like it is now; but you can also put it back together on 1 line in your script. <? function stripHTML($text) { /* Removes ALL (valid) HTML-tags. */ $expr = "/<\/?\w+((\s+\w+)|(\s+\w+(\s*)=(\s*)"; $expr .= "((\w+)|('.*?')|(\".*?\"))))*(\s*)(\/?)>/"; return preg_replace($expr, "", $text); } // All examples will print: "Hello world!" print(stripHTML("<tag>Hello world!</tag>")); print(stripHTML("<tag attr>Hello world!</tag>")); print(stripHTML("<tag attr=value>Hello world!</tag>")); print(stripHTML("<tag attr='value'>Hello world!</tag>")); print(stripHTML("<tag attr=\"value\">Hello world!</tag>")); print(stripHTML("<tag attr attr=value attr='value' attr=\"value\">Hello world!</tag>")); print(stripHTML("<tag />Hello world!")); ?> ---- Manual Page -- http://www.php.net/manual/en/function.strip-tags.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55427 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55427&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55427&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55427&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55427&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55427&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55427&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55427&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92999) next »