首页  >  文章  >  后端开发  >  如何使用php 替换敏感字符串的相关操作

如何使用php 替换敏感字符串的相关操作

jacklove
jacklove原创
2018-06-09 16:20:251409浏览

StrFilter.class.php

<?php
/** string filter class
* Date:     2013-01-09
* Author:   fdipzone
* Ver:      v1.0
*
* Func:
* public  replace            替换非法字符
* public  check              检查是否含有非法字符
* private protect_white_list 保护白名单
* private resume_white_list  还原白名单
* private getval             白名单 key转为value
*/
class StrFilter{ // class start
    private $_white_list = array();
    private $_black_list = array();
    private $_replacement = &#39;*&#39;;
    private $_LTAG = &#39;[[##&#39;;
    private $_RTAG = &#39;##]]&#39;;
    /**
    * @param Array  $white_list
    * @param Array  $black_list
    * @param String $replacement
    */
    public function __construct($white_list=array(), $black_list=array(), $replacement=&#39;*&#39;){
        $this->_white_list = $white_list;
        $this->_black_list = $black_list;
        $this->_replacement = $replacement;
    }
    /** 替换非法字符
    * @param  String $content 要替換的字符串
    * @return String          替換后的字符串
    */
    public function replace($content){
        if(!isset($content) || $content==&#39;&#39;){
            return &#39;&#39;;
        }
        // protect white list
        $content = $this->protect_white_list($content);
        // replace black list
        if($this->_black_list){
            foreach($this->_black_list as $val){
                $content = str_replace($val, $this->_replacement, $content);
            }
        }
        // resume white list
        $content = $this->resume_white_list($content);
        return $content;
    }
    /** 检查是否含有非法自符
    * @param  String $content 字符串
    * @return boolean
    */
    public function check($content){
        if(!isset($content) || $content==&#39;&#39;){
            return true;
        }
        // protect white list
        $content = $this->protect_white_list($content);
        // check
        if($this->_black_list){
            foreach($this->_black_list as $val){
                if(strstr($content, $val)!=&#39;&#39;){
                    return false;
                }
            }
        }
        return true;
    }
    /** 保护白名单
    * @param  String $content 字符串
    * @return String
    */
    private function protect_white_list($content){
        if($this->_white_list){
            foreach($this->_white_list as $key=>$val){
                $content = str_replace($val, $this->_LTAG.$key.$this->_RTAG, $content);
            }
        }
        return $content;
    }
    /** 还原白名单
    * @param  String $content
    * @return String
    */
    private function resume_white_list($content){
        if($this->_white_list){
            $content = preg_replace_callback("/\[\[##(.*?)##\]\].*?/si", array($this, &#39;getval&#39;), $content);
        }
        return $content;
    }
    /** 白名单 key还原为value
    * @param  Array  $matches 匹配white_list的key
    * @return String white_list val
    */
    private function getval($matches){
        return isset($this->_white_list[$matches[1]])? $this->_white_list[$matches[1]] : &#39;&#39;; // key->val
    }
} // class end
?>

demo

<?php
	header("content-type:text/html;charset=utf8");
	require("StrFilter.class.php");
	$white = array(&#39;屌丝&#39;, &#39;曹操&#39;);
	$black = array(&#39;屌&#39;, &#39;操&#39;);
	$content = "我操,曹操你是屌丝,我屌你啊";
	$obj = new StrFilter($white, $black);
	echo $obj->replace($content);
?>

本文讲解了如何使用php 替换敏感字符串的相关操作,更多相关内容请关注php中文网。

相关推荐:

关于PHP 遍历文件夹及文件类及处理类

如何通过php 获取页面中的指定内容类

php 支持断点续传的文件下载类 的相关讲解

以上是如何使用php 替换敏感字符串的相关操作的详细内容。更多信息请关注PHP中文网其他相关文章!

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