首页 > 代码库 > 去除重复嵌套的html标签函数

去除重复嵌套的html标签函数

去除重复嵌套的html标签

 1 function strip_multi_tags($str, $tag = ‘div‘){ 2     preg_match_all(‘/<‘.$tag.‘>|<\/‘.$tag.‘>/is‘, $str, $matched); 3     $divs   = $matched[0]; 4     $pairs  = array(); 5     foreach($divs as $k => $v){ 6         if($v == ‘<‘.$tag.‘>‘){ 7             $begin_keys[] = $k; 8         } else { 9             $bk = array_pop($begin_keys);10             if(!is_null($bk)){11                 $pairs[$bk] = $k;12             }13         }14     }15 16     $datas  = preg_split(‘/<‘.$tag.‘>|<\/‘.$tag.‘>/is‘, $str);17     $result = ‘‘;18     foreach($datas as $k => $v){19         if(isset($datas[$k+1]) && strlen(trim($datas[$k+1])) == 0 20             && isset($pairs[$k]) && isset($pairs[$k+1]) 21             && ($k+1 + $pairs[$k+1]) == ($k + $pairs[$k]))22         {23             unset($divs[$k], $divs[$pairs[$k]]);24         }25         $result .= (strlen(trim($v))? $v: ‘‘). (isset($divs[$k])? $divs[$k]: ‘‘);26     }27     return $result;28 }

 

去除重复嵌套的html标签函数