龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php ThinkPHP敏感词汇过滤

时间:2014-11-11 12:13来源:网络整理 作者:网络 点击:
分享到:
ThinkPHP敏感词汇过滤 如果内容中包含敏感词汇,则返回False,否则返回True。很简单的代码。请将文件放置于 quot;项目/ORG/SensitiveFilter.class.phpquot;下。其中 “ SensitiveThesaurus.php”是一个敏感

如果内容中包含敏感词汇,则返回False,否则返回True。

很简单的代码。

请将文件放置于 "项目/ORG/SensitiveFilter.class.php"下。

其中 “ SensitiveThesaurus.php”是一个敏感词汇数组,大家可以任意添加内容。

<?php
/**
 * 敏感词汇过滤
 * 调用方式
 * if(false === SensitiveFilter::filter($content)){
 *      error("含有敏感词汇");
 * }
 */

class SensitiveFilter extends Think{

    public static $wordArr = array();
    public static $content = "";

    /**
     * 处理内容
     * @param $content
     *
     * @return bool
     */
    public static function filter($content){
        if($content=="") return false;
        self::$content = $content;
        empty(self::$wordArr)?self::getWord():"";
        foreach ( self::$wordArr as $row){
            if (false !== strstr(self::$content,$row)) return false;
        }
        return true;
    }

    public static function getWord(){
        self::$wordArr = include 'SensitiveThesaurus.php';
    }

}
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接