note 86964 added to function.strip-tags
| From: | mariusz dot tarnaskiatwpdotpl at osu1 dot php dot net | Date: | Wed, 12 Nov 2008 16:05:25 +0000 |
| Subject: | note 86964 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146448@lists.php.net to get a copy of this message | ||
Hi. I made a function that removes the HTML tags along with their contents:
Function:
<?php
function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags)
.')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags)
.')\b.*?>.*?</\1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
return $text;
}
?>
Sample text:
$text = '<b>sample</b> text with <div>tags</div>';
Result for strip_tags($text):
sample text with tags
Result for strip_tags_content($text):
text with
Result for strip_tags_content($text, '<b>'):
<b>sample</b> text with
Result for strip_tags_content($text, '<b>', TRUE);
text with <div>tags</div>
I hope that someone is useful :) The exact explanation for Polish PHP programmers at http://www.tarnaski.eu/blog/rozszerzone-strip_tags/
----
Server IP: 82.177.164.100
Probable Submitter: 62.141.244.141
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/86964
Del: integrated -- https://master.php.net/note/delete/86964/integrated
Del: useless -- https://master.php.net/note/delete/86964/useless
Del: bad code -- https://master.php.net/note/delete/86964/bad+code
Del: spam -- https://master.php.net/note/delete/86964/spam
Del: non-english -- https://master.php.net/note/delete/86964/non-english
Del: in docs -- https://master.php.net/note/delete/86964/in+docs
Del: other reasons-- https://master.php.net/note/delete/86964
Reject -- https://master.php.net/note/reject/86964
Search -- https://master.php.net/manage/user-notes.php