• 注册
  • 前端后端 前端后端 关注:1 内容:70

    PHP 常用正则表达式(过滤常用标签)

  • 查看作者
  • 打赏作者
  • 帅气冲天

    问题前言

    在 php 项目中,经常要用到一些过滤标签的正则表达式,可以收藏一下备用:

    解决方案

    $str=preg_replace("/\s+/", " ", $str);           // 过滤 多余回车
    $str=preg_replace("/<[ ]+/si","<",$str);         // 过滤 <__("<"号后面带空格)
    $str=preg_replace("/<\!–.*?–>/si","",$str);    // 注释
    $str=preg_replace("/<(\!.*?)>/si","",$str);      // 过滤 DOCTYPE
    $str=preg_replace("/<(\/?html.*?)>/si","",$str); // 过滤 html   标签
    $str=preg_replace("/<(\/?head.*?)>/si","",$str); // 过滤 head   标签
    $str=preg_replace("/<(\/?meta.*?)>/si","",$str); // 过滤 meta   标签
    $str=preg_replace("/<(\/?body.*?)>/si","",$str); // 过滤 body   标签
    $str=preg_replace("/<(\/?link.*?)>/si","",$str); // 过滤 link   标签
    $str=preg_replace("/<(\/?form.*?)>/si","",$str); // 过滤 form   标签
    $str=preg_replace("/cookie/si","COOKIE",$str);   // 过滤 COOKIE 标签
    $str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str);    // 过滤 applet  标签
    $str=preg_replace("/<(\/?applet.*?)>/si","",$str);                     // 过滤 applet  标签
    $str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str);      // 过滤 style   标签
    $str=preg_replace("/<(\/?style.*?)>/si","",$str);                      // 过滤 style   标签
    $str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str);      // 过滤 title   标签
    $str=preg_replace("/<(\/?title.*?)>/si","",$str);                      // 过滤 title   标签
    $str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str);    // 过滤 object  标签
    $str=preg_replace("/<(\/?objec.*?)>/si","",$str);                      // 过滤 object  标签
    $str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str);// 过滤 noframes标签
    $str=preg_replace("/<(\/?noframes.*?)>/si","",$str);                   // 过滤 noframes标签
    $str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str);  // 过滤 frame   标签
    $str=preg_replace("/<(\/?i?frame.*?)>/si","",$str);                    // 过滤 frame   标签
    $str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str);    // 过滤 script  标签
    $str=preg_replace("/<(\/?script.*?)>/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  标签

    使用说明

    请根据自己实际情况做相应修改

    请登录之后再进行评论

    登录

    我关注的论坛

    暂没有数据

  • 发布内容
  • 做任务
  • 偏好设置
  • 直达底部
  • 帖子间隔 侧栏位置: