In sometime in your string close tag missing in this case it need to close tag. This function help you
<?php 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; } $str = '<div>This is some text<p> and here is a <strong>bold text then the post stop here....'; echo closetags($str);
This is result :
Normal text with missing close tag : <div>This is some text<p> and here is a <strong>bold text then the post stop here.... Then result is : <div>This is some text<p> and here is a <strong>bold text then the post stop here....</strong></p></div>
so this function automatic close tag.
Resource from :
https://stackoverflow.com/questions/3810230/close-open-html-tags-in-a-string
if you face any problem then please inform me.