找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1045|回复: 0
打印 上一主题 下一主题

php过滤HTML标签、属性等正则表达式汇总

[复制链接]

2647

主题

2647

帖子

7881

积分

论坛元老

Rank: 8Rank: 8

积分
7881
跳转到指定楼层
楼主
发表于 2018-2-14 05:54:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            
$str=preg_replace("/\s+/", " ", $str); //过滤多余回车
$str=preg_replace("//si","",$str); //注释
$str=preg_replace("//si","",$str); //过滤DOCTYPE
$str=preg_replace("//si","",$str); //过滤html标签
$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标签,如javAsCript:alert(
清除空格,换行
function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}
过滤HTML属性
1,过滤所有html标签的正则表达式:
[U]复制代码[/U] 代码如下:
]+>

//过滤所有html标签的属性的正则表达式:

$html = preg_replace("/]*>/","",$html);
3,过滤部分html标签的正则表达式的排除式(比如排除,即不过滤):
[U]复制代码[/U] 代码如下:
]+>
4,过滤部分html标签的正则表达式的枚举式(比如需要过滤等):
[U]复制代码[/U] 代码如下:
]*>
5,过滤部分html标签的属性的正则表达式的排除式(比如排除alt属性,即不过滤alt属性):
[U]复制代码[/U] 代码如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,过滤部分html标签的属性的正则表达式的枚举式(比如alt属性):
[U]复制代码[/U] 代码如下:
(\s)alt=[^\s]*
PS:关于正则,这里再为大家推荐2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
            
            
您可能感兴趣的文章:
  • 最常用的PHP正则表达式收集整理
  • PHP 正则表达式常用函数使用小结
  • PHP匹配连续的数字或字母的正则表达式
  • php中字符串和正则表达式详解
  • php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
  • PHP正则表达式的效率 回溯与固化分组
  • PHP之正则表达式捕获组与非捕获组(详解)
  • PHP正则表达式之捕获组与非捕获组
  • 浅谈PHP正则中的捕获组与非捕获组
  • PHP实现正则表达式分组捕获操作示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端