记录学习与后端知识并分享学习代码过程(会飞的鱼Blog)

php 过滤html正则,常用PHP正则表达式过滤html中的标签

会飞的鱼 0 347 2022年12月9日

常用的PHP通过正则表达式过滤HTML的标签,可以实现在某些情况下,只需要取到文本信息,而剔除富文本编辑器带来的一些接口的无法适应和异常。

网上搜索了一些,直接上代码,欢迎补充。

$str=preg_replace("/\s+/", " ", $str); //过滤多余回车

$str=preg_replace("/

$str=preg_replace("//si","",$str); //注释

$str=preg_replace("//si","",$str); //过滤DOCTYPE

$str=preg_replace("//si","",$str); //过滤html标签

$str=preg_replace("//si","",$str); //过滤br标签

$str=preg_replace("//si","",$str); //过滤head标签

$str=preg_replace("//si","",$str); //过滤meta标签

$str=preg_replace("//si","",$str); //过滤body标签

$str=preg_replace("//si","",$str); //过滤link标签

$str=preg_replace("//si","",$str); //过滤form标签

$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签

$str=preg_replace("/(.*?)/si","",$str); //过滤applet标签

$str=preg_replace("//si","",$str); //过滤applet标签

$str=preg_replace("/(.*?)/si","",$str); //过滤style标签

$str=preg_replace("//si","",$str); //过滤style标签

$str=preg_replace("/(.*?)/si","",$str); //过滤title标签

$str=preg_replace("//si","",$str); //过滤title标签

$str=preg_replace("/(.*?)/si","",$str); //过滤object标签

$str=preg_replace("//si","",$str); //过滤object标签

$str=preg_replace("/(.*?)/si","",$str); //过滤noframes标签

$str=preg_replace("//si","",$str); //过滤noframes标签

$str=preg_replace("/(.*?)/si","",$str); //过滤frame标签

$str=preg_replace("//si","",$str); //过滤frame标签

$str=preg_replace("/(.*?)/si","",$str); //过滤script标签

$str=preg_replace("//si","",$str); //过滤script标签

$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签

$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签

$str=preg_replace("/on([a-z]+)\s*=/si","On\1=",$str); //过滤script标签

$str=preg_replace("//si","&#",$str); //过滤script标签

本文由 @会飞的鱼 于 2022-12-9 发布在 会飞的鱼Blog,如无特别说明,本博文章均为原创,转载请保留出处。

网友评论

    暂无评论

会飞的鱼 V

一条会飞的鱼!

745 文章
7274 评论
1069 万 阅读
8年 博龄
最新文章
最新评论
嘻嘻嘻
1个月前 (2024-03-19)

ThinkPHP实现用户注册、登录模块

标签

会飞的鱼 在线咨询

在线时间:9:00-22:00
周六、周日:14:00-22:00