public static function repair($html, $use_tidy = true)
    {
        if (function_exists('tidy_repair_string') && $use_tidy) {
            $TidyConfig = [
                'indent' => true,
                'output-xhtml' => true,
                'show-body-only' => true,
                'wrap' => false,
            ];

            return tidy_repair_string($html, $TidyConfig, 'utf8');
        } else {
            $arr_single_tags = ['meta', 'img', 'br', 'link', 'area'];

            // Put all opened tags into an array
            preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result);
            $openedtags = $result[1];

            // Put all closed tags into an array
            preg_match_all("#</([a-z]+)>#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)) {
                    if (!in_array($openedtags[$i], $arr_single_tags)) {
                        $html .= "</" . $openedtags[$i] . ">";
                    }
                } else {
                    unset($closedtags[array_search($openedtags[$i], $closedtags)]);
                }
            }

            return $html;
        }
    }
public static function repair($html, $use_tidy = true)
    {
        if (function_exists('tidy_repair_string') && $use_tidy) {
            $TidyConfig = [
                'indent' => true,
                'output-xhtml' => true,
                'show-body-only' => true,
                'wrap' => false,
            ];

            return tidy_repair_string($html, $TidyConfig, 'utf8');
        } else {
            $arr_single_tags = ['meta', 'img', 'br', 'link', 'area'];

            // Put all opened tags into an array
            preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result);
            $openedtags = $result[1];

            // Put all closed tags into an array
            preg_match_all("#</([a-z]+)>#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)) {
                    if (!in_array($openedtags[$i], $arr_single_tags)) {
                        $html .= "</" . $openedtags[$i] . ">";
                    }
                } else {
                    unset($closedtags[array_search($openedtags[$i], $closedtags)]);
                }
            }

            return $html;
        }
    }
public static function repair ( $ html , $ use_tidy = true ) { if ( function_exists ( 'tidy_repair_string' ) && $ use_tidy ) { $ TidyConfig = [ 'indent' => true , 'output-xhtml' => true , 'show-body-only' => true , 'wrap' => false , ] ; return tidy_repair_string ( $ html , $ TidyConfig , 'utf8' ) ; } else { $ arr_single_tags = [ 'meta' , 'img' , 'br' , 'link' , 'area' ] ; // Put all opened tags into an array preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU" , $ html , $ result ) ; $ openedtags = $ result [ 1 ] ; // Put all closed tags into an array preg_match_all ( "#</([a-z]+)>#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 ) ) { if ( ! in_array ( $ openedtags [ $ i ] , $ arr_single_tags ) ) { $ html .= "</" . $ openedtags [ $ i ] . ">" ; } } else { unset ( $ closedtags [ array_search ( $ openedtags [ $ i ] , $ closedtags ) ] ) ; } } return $ html ; } }
Repair HTML. If Tidy not exists, use repair function.

@param   string  $html     The HTML string to repair.
@param   boolean $use_tidy Force tidy or not.

@return  string  Repaired HTML.
Repair HTML . If Tidy not exists use repair function .