这是我之前使用过的功能,效果很好:
function closetags($html) { preg_match_all('#<(?!meta|img|br|hr|inputb)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;}但是就我个人而言,我不会使用regexp而是使用Tidy之类的库来实现。这将类似于以下内容:
$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;



