Home  >  Article  >  Backend Development  >  PHP implementation of web page HTML tag completion method_PHP tutorial

PHP implementation of web page HTML tag completion method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:25698browse

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

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

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

<pre class="code"> <pre class="code"><pre class="code">

function closetags($html) {

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

if (!in_array($openedtags[$i], $closedtags)) {

$html .= ''.$openedtags[$i].'>';

} else {

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

}

}

return $html;

}

<pre class="code"><strong>解析: </strong>

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页面给包含进去了,我们可以写个函...
Statement:
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