Mar 31, 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,'<table><a>');
*/
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;
}
?>

2 Reactions to this post

Add Comment
  1. smith said... July 30, 2010 5:02 PM

    Hey,
    Loving your blog, awesome tips on this you have here. I
    would just like to ask you some questions privately, mind
    contacting me at sales@ company logo design.com
    logo design

  2. joey said... September 19, 2010 12:47 PM

    thanks for the tips