欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 过滤非法与特殊字符串的方法

程序员文章站 2022-05-31 09:54:31
...
本文介绍下,php实现的过滤非法字符与特殊字符串的方法,有需要的朋友参考下。

提供几段函数,实现非法字符串、特殊字符串的过滤方法。

代码:

'),array('','?>'),$html);  
    $pattern=array(  
   "''si",  
   "''si",  
   "']*?>'si",  
   "''si",  
   "']*?>'si"  
   );  
   $replace=array("","","","","");  
   return   preg_replace($pattern,$replace,$html);  
 }  
 /* Remove JS/CSS/IFRAME/FRAME 过滤JS/CSS/IFRAME/FRAME/XSS等恶意攻击代码(可安全使用) 
  * Return string 
  */  
 function cleanJsCss($html){  
    $html=trim($html);  
    $html=preg_replace('/\0+/', '', $html);  
$html=preg_replace('/(\\\\0)+/', '', $html);  
$html=preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"\\1;",$html);  
$html=preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"\\1\\2;",$html);  
$html=preg_replace("/%u0([a-z0-9]{3})/i", "\\1;", $html);  
$html=preg_replace("/%([a-z0-9]{2})/i", "\\1;", $html);  
    $html=str_replace(array('','?>'),array('','?>'),$html);  
   $html=preg_replace('#\t+#',' ',$html);  
$scripts=array('javascript','vbscript','script','applet','alert','document','write','cookie','window');  
foreach($scripts as $script){  
    $temp_str="";  
    for($i=0;$i#si", "", $html);  
$html=preg_replace("##si", "", $html);  
$html=preg_replace('#(]*?)(onblur|onchange|onclick|onfocus|onload|onmouseover|onmouseup|onmousedown|onselect|onsubmit|onunload|
onkeypress|onkeydown|onkeyup|onresize)[^>]*>#is',"\\1>",$html);  
//$html=preg_replace('#]*)>#is', "", $html);  
$html=preg_replace('#]*)>#is', "", $html);  
$html=preg_replace('#(alert|cmd|passthru|eval|exec|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si',
 "\\1\\2(\\3)", $html);  
$bad=array(  
'document.cookie'   => '',  
'document.write'    => '',  
'window.location'   => '',  
"javascript\s*:"    => '',  
"Redirect\s+302"    => '',  
''                => '-->'  
);  
foreach ($bad as $key=>$val){  
    $html=preg_replace("#".$key."#i",$val,$html);  
}  
   return   $html;  
 }  
 //过滤html标签以及敏感字符  
  
 function cleanHtml($html){  
    return cleanYellow(htmlspecialchars($html));  
 }  
 //过滤部分HTML标签  
  
 function cleanFilter($html){  
    $html=trim($html);  
    $html=preg_replace("/

]*?>/is","

",$html); $html=preg_replace("/

]*?>/is","
",$html); $html=preg_replace("/
    ]*?>/is","
      ",$html); $html=preg_replace("/
    • ]*?>/is","
    • ",$html); $html=preg_replace("/]*?/is","",$html); $html=preg_replace("/]*?>(.*)?/is","\$发达",$html); $html=preg_replace("/]*?>/is","
      ",$html); $html=preg_replace("/]*?>/is","",$html); $html=preg_replace("/
      ]*?>/is"," ",$html); $html=preg_replace("/
        ]*?>/is","
          ",$html); $html=preg_replace("/
      ]*?>/is","",$html); $html=preg_replace("/]*?>/is","",$html); return $html; } //过滤非法的敏感字符串 function cleanYellow($txt){ $txt=str_replace( array("黄色","性爱","做爱","我日","我草","我靠","尻","*","胡*","*", "*","*","研究生考试","性生活","色情","情色","我考","麻痹","妈的","阴道", "淫","奸","阴部","爱液","阴液","臀","色诱","煞笔","傻比","阴茎","*","性交","阴毛","*"), array("*1*","*2*","*3*","*4*","*5*","*6*","*7*","*8*","*9*","*10*", "*11*","*12*","*13*","*14*","*15*","*16*","*17*","*18*","*19*","*20*", "*21*","*22*","*23*","*24*","*25*","*26*","*27*","*28*","*29*","*30*","*31*","*32*","*33*","*34*"), $txt); return $txt; } //过滤敏感字符串以及恶意代码 function cleanAll($html){ return cleanYellow(cleanJsCss($html)); } //全半角字符替换 function setFilter($html){ $arr=array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[', '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']', '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => ' '>', '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-', ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.', ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|', '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"', ' ' => ' '); return strtr($html,$arr); } //搜集整理:bbs.it-home.org ?>
      php 过滤非法与特殊字符串的方法

      声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

      相关文章

      相关视频


      网友评论

      文明上网理性发言,请遵守 新闻评论服务协议

      我要评论
    • php 过滤非法与特殊字符串的方法

专题推荐

作者信息
php 过滤非法与特殊字符串的方法

认证0级讲师

推荐视频教程
  • php 过滤非法与特殊字符串的方法javascript初级视频教程
  • php 过滤非法与特殊字符串的方法jquery 基础视频教程
  • 视频教程分类
    相关标签: php 过滤非法与特殊字符串的方法

    上一篇: PHP封装的MSSql操作类完整实例,php封装mssql实例

    下一篇: ThinkPHP 3.2.3实现页面静态化功能的方法

    推荐阅读