note 65880 deleted from function.strip-tags by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 16:00:21 +0000 |
| Subject: | note 65880 deleted from function.strip-tags by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154737@lists.php.net to get a copy of this message | ||
Note Submitter: xyexz at yahoo dot com
----
I have found with this function that sometimes it will only remove the first carrot from a tag and
leave the rest of the tag in the string, which obviously isn't what I'm looking for.
EX:
<?php
//Returns "tag>test/tag>"
echo strip_tags("<tag>test</tag>");
?>
I'm trying to strip_tags on a string I'm importing from xml so perhaps it has something to
do with that but if you've run into this same issue I've written a function to fix it once
and for all!
<?php
function theRealStripTags($string)
{
//while there are tags left to remove
while(strstr($string, '>'))
{
//find position of first carrot
$currentBeg = strpos($string, '<');
//find position of end carrot
$currentEnd = strpos($string, '>');
//find out if there is string before first carrot
//if so save it in $tmpstring
$tmpStringBeg = @substr($string, 0, $currentBeg);
//find out if there is string after last carrot
//if so save it in $tmpStringEnd
$tmpStringEnd = @substr($string, $currentEnd + 1, strlen($string));
//cut the tag from the string
$string = $tmpStringBeg.$tmpStringEnd;
}
return $string;
}
//Returns "test"
echo theRealStripTags('<tag>test</tag>');
?>