public static function closeOpenTags($html)
    {
        preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result);
        $openedTags = $result[1];
        preg_match_all("#</([a-z]+)>#iU", $html, $result);
        $closedTags = $result[1];
        $lenOpened = count($openedTags);

        if (count($closedTags) == $lenOpened) {
            return $html;
        }

        $openedTags = array_reverse($openedTags);
        for ($i = 0; $i < $lenOpened; $i++) {
            if (!in_array($openedTags[$i], $closedTags)) {
                $html .= '</' . $openedTags[$i] . '>';
            } else {
                unset($closedTags[array_search($openedTags[$i], $closedTags)]);
            }
        }

        return $html;
    }
public static function closeOpenTags($html)
    {
        preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result);
        $openedTags = $result[1];
        preg_match_all("#</([a-z]+)>#iU", $html, $result);
        $closedTags = $result[1];
        $lenOpened = count($openedTags);

        if (count($closedTags) == $lenOpened) {
            return $html;
        }

        $openedTags = array_reverse($openedTags);
        for ($i = 0; $i < $lenOpened; $i++) {
            if (!in_array($openedTags[$i], $closedTags)) {
                $html .= '</' . $openedTags[$i] . '>';
            } else {
                unset($closedTags[array_search($openedTags[$i], $closedTags)]);
            }
        }

        return $html;
    }
public static function closeOpenTags ( $ html ) { preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU" , $ html , $ result ) ; $ openedTags = $ result [ 1 ] ; preg_match_all ( "#</([a-z]+)>#iU" , $ html , $ result ) ; $ closedTags = $ result [ 1 ] ; $ lenOpened = count ( $ openedTags ) ; if ( count ( $ closedTags ) == $ lenOpened ) { return $ html ; } $ openedTags = array_reverse ( $ openedTags ) ; for ( $ i = 0 ; $ i < $ lenOpened ; $ i ++ ) { if ( ! in_array ( $ openedTags [ $ i ] , $ closedTags ) ) { $ html .= '</' . $ openedTags [ $ i ] . '>' ; } else { unset ( $ closedTags [ array_search ( $ openedTags [ $ i ] , $ closedTags ) ] ) ; } } return $ html ; }
Attempts to close any unclosed HTML tag.

@param string $html HTML content to fix
@return string
Attempts to close any unclosed HTML tag .