Reg expression to remove empty Tags (any of them)?

后端 未结 3 976
醉酒成梦
醉酒成梦 2020-12-18 17:21

I like to remove any empty html tag which is empty or containing spaces.

something like to get:

$string = \"text&         


        
3条回答
  •  醉酒成梦
    2020-12-18 17:29

    function stripEmptyTags ($result)
    {
        $regexps = array (
        '~<(\w+)\b[^\>]*>\s*~',
        '~<\w+\s*/>~'
        );
    
        do
        {
            $string = $result;
            $result = preg_replace ($regexps, '', $string);
        }
        while ($result != $string);
    
        return $result;
    }
    
    
    $string = "text 


    "; echo stripEmptyTags ($string);

提交回复
热议问题