php 替换敏感字符串

来源:互联网 发布:淘宝售假仅退款不退货 编辑:程序博客网 时间:2024/05/18 00:33
StrFilter.class.php
[php] view plain copy
  1. <?php  
  2. /** string filter class 
  3. * Date:     2013-01-09 
  4. * Author:   fdipzone 
  5. * Ver:      v1.0 
  6. * 
  7. * Func: 
  8. * public  replace            替换非法字符 
  9. * public  check              检查是否含有非法字符 
  10. * private protect_white_list 保护白名单 
  11. * private resume_white_list  还原白名单 
  12. * private getval             白名单 key转为value 
  13. */  
  14. class StrFilter{ // class start  
  15.   
  16.     private $_white_list = array();  
  17.     private $_black_list = array();  
  18.     private $_replacement = '*';  
  19.     private $_LTAG = '[[##';  
  20.     private $_RTAG = '##]]';  
  21.   
  22.   
  23.     /** 
  24.     * @param Array  $white_list 
  25.     * @param Array  $black_list 
  26.     * @param String $replacement 
  27.     */  
  28.     public function __construct($white_list=array(), $black_list=array(), $replacement='*'){  
  29.         $this->_white_list = $white_list;  
  30.         $this->_black_list = $black_list;  
  31.         $this->_replacement = $replacement;  
  32.     }  
  33.   
  34.   
  35.     /** 替换非法字符 
  36.     * @param  String $content 要替換的字符串 
  37.     * @return String          替換后的字符串 
  38.     */  
  39.     public function replace($content){  
  40.   
  41.         if(!isset($content) || $content==''){  
  42.             return '';  
  43.         }  
  44.   
  45.         // protect white list  
  46.         $content = $this->protect_white_list($content);  
  47.   
  48.         // replace black list  
  49.         if($this->_black_list){  
  50.             foreach($this->_black_list as $val){  
  51.                 $content = str_replace($val$this->_replacement, $content);  
  52.             }  
  53.         }  
  54.   
  55.         // resume white list  
  56.         $content = $this->resume_white_list($content);  
  57.   
  58.         return $content;  
  59.     }  
  60.   
  61.   
  62.     /** 检查是否含有非法自符 
  63.     * @param  String $content 字符串 
  64.     * @return boolean 
  65.     */  
  66.     public function check($content){  
  67.   
  68.         if(!isset($content) || $content==''){  
  69.             return true;  
  70.         }  
  71.   
  72.         // protect white list  
  73.         $content = $this->protect_white_list($content);  
  74.   
  75.         // check  
  76.         if($this->_black_list){  
  77.             foreach($this->_black_list as $val){  
  78.                 if(strstr($content$val)!=''){  
  79.                     return false;  
  80.                 }  
  81.             }  
  82.         }  
  83.   
  84.         return true;  
  85.     }  
  86.   
  87.   
  88.     /** 保护白名单 
  89.     * @param  String $content 字符串 
  90.     * @return String 
  91.     */  
  92.     private function protect_white_list($content){  
  93.         if($this->_white_list){  
  94.             foreach($this->_white_list as $key=>$val){  
  95.                 $content = str_replace($val$this->_LTAG.$key.$this->_RTAG, $content);  
  96.             }  
  97.         }  
  98.         return $content;  
  99.     }  
  100.   
  101.   
  102.     /** 还原白名单 
  103.     * @param  String $content 
  104.     * @return String 
  105.     */  
  106.     private function resume_white_list($content){  
  107.         if($this->_white_list){  
  108.             $content = preg_replace_callback("/
    \[##(.*?)##
    \].*?/si"array($this'getval'), $content);  
  109.         }  
  110.         return $content;  
  111.     }  
  112.   
  113.   
  114.     /** 白名单 key还原为value 
  115.     * @param  Array  $matches 匹配white_list的key 
  116.     * @return String white_list val 
  117.     */  
  118.     private function getval($matches){  
  119.         return isset($this->_white_list[$matches[1]])? $this->_white_list[$matches[1]] : ''// key->val  
  120.     }  
  121.   
  122. // class end  
  123.   
  124. ?>  
demo
[php] view plain copy
  1. <?php  
  2.     header("content-type:text/html;charset=utf8");  
  3.   
  4.     require("StrFilter.class.php");  
  5.   
  6.     $white = array('屌丝''曹操');  
  7.     $black = array('屌''操');  
  8.   
  9.     $content = "我操,曹操你是屌丝,我屌你啊";  
  10.   
  11.     $obj = new StrFilter($white$black);  
  12.     echo $obj->replace($content);  
  13. ?> 
原创粉丝点击