Sometimes we come to the situation when we need to strip one specific HTML tag from a string and the PHP function
Here is a quick little function. It takes two parameters, the tag to be removed and the string from which it is to be removed.
Call function like this :
$result = strip_single('a',$string);
It will remove all <a> tags from string.
strip_tags()
doesn’t work the way we want it to. strip_tags()
allows for certain exclusions, but why would we use that when we only want to exclude one tag and include all other tags? Especially when we can use preg_replace()
.Here is a quick little function. It takes two parameters, the tag to be removed and the string from which it is to be removed.
function strip_single($tag,$string){
$string=preg_replace('/<'.$tag.'[^>]*>/i', '', $string);
$string=preg_replace('/<\/'.$tag.'>/i', '', $string);
return $string;
}
Call function like this :
$result = strip_single('a',$string);
It will remove all <a> tags from string.
Posting Komentar