您的位置:首页 > 教程笔记 > 综合教程

zblog检查并闭合html代码中的各种未闭合的成对标签

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

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;
}

相关推荐