Close Open HTML Tags in a String

Close open HTML tags in a string

Here is a function i've used before, which works pretty well:

function closetags($html) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}

Personally though, I would not do it using regexp but a library such as Tidy. This would be something like the following:

$str = '<p>This is some text and here is a <strong>bold text then the post stop here....</p>';
$tidy = new Tidy();
$clean = $tidy->repairString($str, array(
'output-xml' => true,
'input-xml' => true
));
echo $clean;

Searching for all open HTML tags and closing them [PHP]

You can use DOMDocument for this:

$yourText = "<div><span>Text<em>!";

$doc = new DOMDocument();
$doc->loadHTML($yourText, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$yourText = $doc->saveHTML();

echo $yourText;

Out:

<div><span>Text<em>!</em></span></div>

Edit: If your text is truncated and it happens to end with something like <p>This is long text</, DOMDocument will happily strip that truncated tag out and rebuild it. It will raise a warning about what it did, however. To suppress that warning you can put libxml_use_internal_errors(true); prior to loadHTML().

How to close unclosed HTML Tags?

Found a great answer for this one:

Use PHP 5 and use the loadHTML() method of the DOMDocument object. This auto parses badly formed HTML and a subsequent call to saveXML() will output the valid HTML. The DOM functions can be found here:

http://www.php.net/dom

The usage of this:

$doc = new DOMDocument();
$doc->loadHTML($yourText);
$yourText = $doc->saveHTML();


Related Topics



Leave a reply



Submit