note 55427 deleted from function.strip-tags by sean
| From: | sean@php.net | Date: | Wed, 03 Aug 2005 14:13:52 +0000 |
| Subject: | note 55427 deleted from function.strip-tags by sean | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-93007@lists.php.net to get a copy of this message | ||
Note Submitter: me dot wieringa at home dot nl
Reason: useless
----
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!"));
?>