PHP实现补齐关闭的HTML标签

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP实现补齐关闭的HTML标签脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP实现补齐关闭的HTML标签分享给大家供大家参考,具体如下:

很多时候,在我们做文章截取摘要的时候,如果出现HTML的内容,会出现截取文章没有结束的HTML标签。这样的情况下就会出现页面样式错乱的问题。这 个时候我们需要的就是把缺少的结束标签加批量加上。在www.PHP.net官网看到一个比较好处理的一个函数,展示如下:

<PRe class="brush:PHP;"> function CloseTags($html) { // strip fraction of open or close tag from end (e.g. if we take first x characters,we might cut off a tag at the end!) $html = preg_replace('/<[^>]*$/','',$html); // ending with fraction of open tag // put open tags into an array preg_match_all('#<([a-z]+)(?:>#iU',$html,$result); $opentags = $result[1]; // put all closed tags into an array preg_match_all('##iU',$result); $closetags = $result[1]; $len_opened = count($opentags); // if all tags are closed,we can return if (count($closetags) == $len_opened) { return $html; } // close tags in reverse order that they were opened $opentags = array_reverse($opentags); // self closing tags $sc = array('br','input','img','hr','Meta','link'); //,'frame','iframe','param','area','base','basefont','col' // should not skip tags that can have content inside! for ($i=0; $i < $len_oPEned;="" $i++)="" {="" $ot="strtolower($opentags[$i]);" if="" (!in_array($opentags[$i],$closetags)="" &&="" !in_array($ot,$sc))="" {="" $html="" .=''; } else { unset($closetags[array_seArch($opentags[$i],$closetags)]); } } return $html; }

@H_502_8@

脚本宝典总结

以上是脚本宝典为你收集整理的PHP实现补齐关闭的HTML标签全部内容,希望文章能够帮你解决PHP实现补齐关闭的HTML标签所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。