200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 2种PHP实现网页内容html标签补全和过滤办法

2种PHP实现网页内容html标签补全和过滤办法

时间:2021-09-16 19:36:04

相关推荐

2种PHP实现网页内容html标签补全和过滤办法

web前端|html教程

html标签,补全,过滤

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

范文文档整站源码采集,vscode的断点调试,ubuntu开机fwupd,osx+tomcat配置,sqlite3增加新字段,爬虫数据库的制作方法,php 按照键值排序,考拉seo文章裂变,网站后台登陆破解,移动端 新闻 模板下载安装lzw

php使HTML标签自动补全,闭合,过滤函数方法一:

易语言图片框翻页源码,阿里云源 ubuntu,优酷弹幕 爬虫,php symlink,母查询seolzw

代码:

淘宝导购站源码,vscode默认用户设置,ubuntu界面扩大,tomcat改端口配置,爬虫单首音乐,php 插入数组指定位置,云南短视频seo专业布局,成品超市网站,后台权限管理系统模板lzw

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;}

closetags()解析:

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

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

php使HTML标签自动补全,闭合,过滤函数方法二:

function checkhtml($html) { $html = stripslashes($html); preg_match_all("/\<([^\/is", $html, $ms); $searchs[] = <; $replaces[] = \; $replaces[] = >; if($ms[1]) {$allowtags = img|font|p|table|tbody|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li;//允许的标签$ms[1] = array_unique($ms[1]);foreach ($ms[1] as $value) { $searchs[] = ""; $value = shtmlspecialchars($value); $value = str_replace(array(\\\,/*), array(.,/.), $value); $value = preg_replace(array("/(javascript|script|eval|behaviour|expression)/i", "/(\s+|"|)on/i"), array(., .), $value); if(!preg_match("/^[\/|\s]?($allowtags)(\s+|$)/is", $value)) {$value = \; } $replaces[] = empty($value)?\:"";} } $html = str_replace($searchs, $replaces, $html); return $html;}//取消HTML代码function shtmlspecialchars($string) { if(is_array($string)) { foreach($string as $key => $val) {$string[$key] = shtmlspecialchars($val); } } else { $string = preg_replace(/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/, &\\1,str_replace(array(&, \", \), array(&, \", \), $string)); } return $string;}

checkhtml($html)解析:

stripslashes():函数删除由addslashes()函数添加的反斜杠。该函数用于清理从数据库或HTML表单中取回的数据。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。