可以使用zblog PHP的CloseTags函数。
检查并闭合html代码中的各种未闭合的成对标签。'br', 'input', 'img', 'hr', 'meta', 'link'等标签不闭合。
语法
点击复制代码 ActionScript
String CloseTags($html)
调用参数
参数类型 |
参数 |
默认值 |
描述 |
string |
$html |
|
html源码 |
返回值
返回已闭合处理的html源码
代码示例
验证字符串是否为邮箱地址
点击复制代码 ActionScript
$html="<p>123456";
$r=CloseTags($html);
echo $r;
输出内容:
点击复制代码 ActionScript
<p>123456</p>
CloseTags函数
点击复制代码 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;
}