php close unclosed html tags

Solutions on MaxInterview for php close unclosed html tags by the best coders in the world

showing results for - "php close unclosed html tags"
Pierce
01 Jan 2020
1function closetags($html) {
2    preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
3    $openedtags = $result[1];
4    preg_match_all('#</([a-z]+)>#iU', $html, $result);
5    $closedtags = $result[1];
6    $len_opened = count($openedtags);
7    if (count($closedtags) == $len_opened) {
8        return $html;
9    }
10    $openedtags = array_reverse($openedtags);
11    for ($i=0; $i < $len_opened; $i++) {
12        if (!in_array($openedtags[$i], $closedtags)) {
13            $html .= '</'.$openedtags[$i].'>';
14        } else {
15            unset($closedtags[array_search($openedtags[$i], $closedtags)]);
16        }
17    }
18    return $html;
19} 
20
similar questions
queries leading to this page
php close unclosed html tags