PHP implementation of web page HTML tag completion method_PHP tutorial

WBOY
Release: 2016-07-13 10:37:25
Original
743 people have browsed it

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

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

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

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="code">
Copy after login
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="code">
Copy after login

function closetags($html) {

preg_match_all('#<(?!meta|img|br|hr|input\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;

}

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="code"><strong>解析: </strong>
Copy after login

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

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735882.htmlTechArticle如果你的网页内容的html标签显示不全,有些表标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包含进去了,我们可以写个函...
Related labels:
source:php.cn
Previous article:php object-oriented_PHP tutorial Next article:How to solve the date time difference problem in PHP_PHP tutorial
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
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!