Home> php教程> php手册> body text

php实现网页HTML标签补全方法

WBOY
Release: 2016-06-21 08:48:31
Original
835 people have browsed it

如果你的网页内容的html标签显示不全,有些表格标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包含进去了,我们可以写个函数方法来补全html标签以及过滤掉无用的html标签.

php实现网页HTML标签补全方法

php使HTML标签自动补全闭合函数方法如下:

       
Copy after login

      
Copy after login

function closetags($html) {

preg_match_all('#<(?!metaimgbrhrinput\b)\b([a-z]+)(?: .*)?(? #iU', $html, $result);

$openedtags = $result[1];

preg_match_all('# #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 .= ' ';

} else {

unset($closedtags[array_search($openedtags[$i], $closedtags)]);

}

}

return $html;

}

解析: 
     
Copy after login

array_reverse() : 此函数将原数组中的元素顺序翻转,创建新的数组并返回。如果第二个参数指定为 true,则元素的键名保持不变,否则键名将丢失。

array_search() : array_search(value,array,strict),此函数与in_array()一样在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。 如果第三个参数strict被指定为 true,则只有在数据类型和值都一致时才返回相应元素的键名。




Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!