How to close unclosed HTML Tags?

后端 未结 8 575
名媛妹妹
名媛妹妹 2020-11-30 01:56

Whenever we are fetching some user inputed content with some editing from the database or similar sources, we might retrieve the portion which only contains the opening tag

8条回答
  •  -上瘾入骨i
    2020-11-30 02:33

    I have solution for php

    #iU", $html, $result );
            $openedtags = $result[1];
    
            #put all closed tags into an array
            preg_match_all ( "##iU", $html, $result );
            $closedtags = $result[1];
            $len_opened = count ( $openedtags );
    
            # all tags are closed
            if( count ( $closedtags ) == $len_opened )
            {
                return $html;
            }
            $openedtags = array_reverse ( $openedtags );
    
            # close tags
            for( $i = 0; $i < $len_opened; $i++ )
            {
                if ( !in_array ( $openedtags[$i], $closedtags ) )
                {
                    $html .= "";
                }
                else
                {
                    unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
                }
            }
            return $html;
        }
        // close opened html tags
    ?>
    

    You can use this function like

       test test"); ?>
    

提交回复
热议问题