Friday, July 9, 2010

PHP function to strip only selected tags

The following function is somewhat same with the php strip_tags function with added functionality to select what tags you want to strip.

function strip_only($str,$tags){
/*
usage:
echostrip_only($str,array('table','a'));
echostrip_only($str,'');
*/
if(!is_array($tags)){
$tags=(strpos($str,'>')!==false? explode('>',str_replace('<','',$tags)):array($tags));
if(
end($tags)=='')array_pop($tags);
}
foreach(
$tags as $tag)
$str=preg_replace('#.$tag.'[^>]*>#is','',$str);
return
$str;
}
?>

No comments: