+ $str : là đoạn html truyền vào để loại bỏ thẻ.
abc
tét ne ”,array(“div”,”b”))
function check_tag($str,$list_tag = array())
{
foreach($list_tag as $k=>$v)
{
$str = preg_replace(“/[<]{1}[\/]{1}[ ]*({$v}){1}[ ]*[(>)]{0,1}/”,”$v>”,$str);
$arr[$k] = preg_split(“/[(<)]{1}[ ]*({$v}){1}[ a-zA-Z \'\"=;:]*[(>)]{1}/”,$str);
$arr1[$k] = preg_split(“/[<]{1}[\/]{1}[ ]*({$v}){1}[ ]*[(>)]{1}/”,$str);
if(count($arr[$k]) > count($arr1[$k]))
{
$key = count($arr[$k]) – count($arr1[$k]);
$count = count($arr[$k]);
for($i=0;$i < $key;$i++)
{
$save[$k][] = $arr[$k][($count-$i-1)];
unset($arr[$k][($count-$i-1)]);
}
if(!empty($save[$k]))
{
$str = implode(“<”.$v.”>”,$arr[$k]).implode(”,$save[$k]);
} else {
$str = implode(“<”.$v.”>”,$arr[$k]);
}
}
if(count($arr[$k]) < count($arr1[$k]))
{
$key = count($arr1[$k]) – count($arr[$k]);
$count = count($arr1[$k]);
for($i=0;$i < $key;$i++)
{
$save[$k][] = $arr1[$k][($i)];
unset($arr1[$k][($i)]);
}
if(isset($save[$k]))
{
$str = implode(”,$save[$k]).implode(“”.$v.”>”,$arr1[$k]);
} else {
$str = implode(“”.$v.”>”,$arr1[$k]);
}
}
}
return $str;
}
(i-php.net)