替换敏感字符串的php代码

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 = '*';
    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  

?>

demo

<?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);
?>

源码下载地址:http://download.csdn.net/detail/fdipzone/5789781

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
, 字符串
, http
, download
, www
源码下载
java中替换敏感词代码、php 敏感词过滤替换、php 敏感词替换、php 字符串替换、php字符串替换函数,以便于您获取更多的相关知识。

时间: 2024-08-03 10:57:20

替换敏感字符串的php代码的相关文章

php实现的替换敏感字符串类实例_php技巧

本文实例讲述了php实现的替换敏感字符串类及其用法,在php程序开发中有着非常广泛的应用价值.分享给大家供大家参考.具体方法如下: StrFilter.class.php类文件如下: <?php /** string filter class * Date: 2013-01-09 * Author: fdipzone * Ver: v1.0 * * Func: * public replace 替换非法字符 * public check 检查是否含有非法字符 * private protect_

php实现的替换敏感字符串类实例

 StrFilter.class.php类文件如下: 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 7

Z-BLOG屏蔽敏感关键字功能的代码

下面是ZBLOG的屏蔽评论中敏感关键字的程序代码,主要用于替换文章评论中的敏感或非法的关键字. 只需要修改一个文件c_system_event.asp,将其中的objComment.Content=Request.Form("inpArticle")修改为objComment.Content=WordFilter(Request.Form("inpArticle")) 然后在c_system_event.asp文件尾部加入代码: Function WordFilte

java中替换去除字符串中的空格/回车/换行符/制表符

用String对象的方法replaceAll就可以了! replaceAll(String regex, String replacement)           使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串. 示例代码:  代码如下 复制代码 public class T3 { public static void main(String args[]) {  String str="aa bb cc";  System.out.prin

JS判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个)_javascript技巧

js判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个) 文本输入时,由于数据库表字段长度限制会导致提交失败,因此想到了此方法验证. 废话不多说上代码: <html> <head> <title>js判断输入字符串长度(汉字算两个字符,字母数字算一个)</title> <style type="text/css"> .pbt { margin-bottom: 10px; } .ie6 .pbt .ftid a, .ie

批量替换ntext字段内容,@textA为要替换的字符串,@textB为替换后的字符串

代码 --批量替换ntext字段内容,@textA为要替换的字符串,@textB为替换后的字符串--exec P_replace_TableNTEXT 'B_Goods','G_Content','GID','61.152.93.172:888','212.95.33.47'create proc P_replace_TableNTEXT(@tableName varchar(50),@ColNTEXT varchar(50),@ColPrimaryKey varchar(50),@textA

Lua中计算、执行字符串中Lua代码的方法

  这篇文章主要介绍了Lua中计算.执行字符串中Lua代码的方法,类似JavaScript中eval函数的功能,在Lua中也可以实现,需要的朋友可以参考下 一.Lua中执行字符串 运行过程中有个问题,我有个字符串,是一个数学表达式,如何计算这个字符串表达式的值呢? 比如,local param = "7*100", 我需要的结果其实是700,但是怎么样直接计算出这个值呢?方法如下 字符串前面 加个 "return" 然后loadstring以后得到一个functio

关于awk替换删除字符串的问题

问题描述 关于awk替换删除字符串的问题 原文件test.txt apple banana (随机不固定一行文本) orange peach pear tangerine watermelon 要求: 1. apple替换成strawberry 2. banana下面一行替换成litchi 3. 从orange开始到tangerine结束的行不要 最终的效果: strawberry banana litchi watermelon 求问大虾们awk应该怎么写? 解决方案 awk 字符串替换 gs

asp.net(c#) 使用Rex正则来生成字符串数组的代码

看这儿.如果你熟悉正则表达式 ,让我们进入正题.这个TOOL的名称叫Regular Expression Exploration. 你可以从这儿下载 .目前的版本是1.0 release. Rex是一个命令行工具, 具体用法可以在CMD下执行便可以看到用法,这个是.net的程序.我们可以引用它,然后用下面的Code来生成我们想要的字符串数组. 复制代码 代码如下: /// <summary> /// Generates the test. /// </summary> /// &l