|
本文实例讲述了PHP实现补齐关闭的HTML标签。分享给大家供大家参考,具体如下:
很多时候,在我们做文章截取摘要的时候,如果出现HTML的内容,会出现截取的文章没有结束的HTML标签。这样的情况下就会出现页面样式错乱的问题。这 个时候我们需要的就是把缺少的结束标签加批量加上。在www.php.net官网看到一个比较好处理的一个函数,展示如下:
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('##iU', $html, $result);
$opentags = $result[1];
// put all closed tags into an array
preg_match_all('##iU', $html, $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 ';
}
else
{
unset($closetags[array_search($opentags[$i], $closetags)]);
}
}
return $html;
}
测试使用的结果:
越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!';
echo CloseTags($content);
/*
返回的结果是:
越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!
*/
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:php下清空字符串中的HTML标签的代码php正则过滤html标签、空格、换行符的代码(附说明)PHP实现HTML标签自动补全代码php使HTML标签自动补全闭合函数代码PHP删除HTMl标签的三种解决方法php去除HTML标签实例PHP中HTML标签过滤技巧php获取网页标题和内容函数(不包含html标签)php 判断字符串中是否包含html标签php对包含html标签的字符串进行截取的函数分享php过滤HTML标签、属性等正则表达式汇总php实现过滤表单提交中html标签的方法php截取html字符串及自动补全html标签的方法PHP实现过滤各种HTML标签php实现html标签闭合检测与修复方法
|
|