php 替换敏感字符串
来源:互联网 发布:淘宝售假仅退款不退货 编辑:程序博客网 时间:2024/05/18 00:33
StrFilter.class.php
demo
- <?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 = '*';
- private $_LTAG = '[[##';
- private $_RTAG = '##]]';
- /**
- * @param Array $white_list
- * @param Array $black_list
- * @param String $replacement
- */
- public function __construct($white_list=array(), $black_list=array(), $replacement='*'){
- $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==''){
- return '';
- }
- // 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==''){
- 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)!=''){
- 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, 'getval'), $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]] : ''; // key->val
- }
- } // class end
- ?>
- <?php
- header("content-type:text/html;charset=utf8");
- require("StrFilter.class.php");
- $white = array('屌丝', '曹操');
- $black = array('屌', '操');
- $content = "我操,曹操你是屌丝,我屌你啊";
- $obj = new StrFilter($white, $black);
- echo $obj->replace($content);
- ?>
阅读全文
0 0
- php 替换敏感字符串
- php 替换敏感字符串
- PHP 敏感词替换
- php 敏感词替换
- 循环替换字符串,可用户替换敏感词
- php替换字符串中的'/'
- php字符串替换函数
- php字符串替换函数
- PHP:替换字符串
- php 字符串替换
- php str_replace 字符串替换
- PHP替换子字符串
- PHP替换字符串-str_replace
- java过滤敏感词实现字符串替换功能
- 2017百度前端编程 敏感词汇 字符串替换
- php将敏感词替换为*的方法
- java字符串替换:大小写敏感和不敏感,一句代码解决!
- java字符串替换:大小写敏感和不敏感,一句代码解决
- 实验四 DPCM编码
- download
- Python语法第3讲:数组
- Windows无法格式化改卷,改卷已脱机, 请尝试首先向改卷分配驱动器号或路径使其联机
- loadrunner GUI界面性能测试
- php 替换敏感字符串
- 数据库最常见的10个安全问题
- 接着很久以前的贪吃蛇
- [JAVA学习笔记-97]ActiveObject模式的Scheduler的关键实现
- Java动态代理的两种实现方法
- HDU3829 (9/600)
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case和条件语句
- Spring jdbcTemplate 的测试 --(三)
- win32(6)--GDI绘图基础