PHP-敏感词汇过滤

方法一:

$sensitive = array(
    '小白', '小黑', 'me', 'you'
);
$badword = array_combine($sensitive,array_fill(0,count($sensitive),'*'));
$string = 'likeyou小白喜欢小黑爱着的大黄';
$str = strtr($string, $badword);
echo $str;

方法二:

function sensitive($list, $string){
    $count = 0; //违规词的个数
    $sensitiveWord = '';  //违规词
    $stringAfter = $string;  //替换后的内容
    $pattern = "/".implode("|",$list)."/i"; //定义正则表达式

    if(preg_match_all($pattern, $string, $matches)){ //匹配到了结果
        $patternList = $matches[0];  //匹配到的数组
        $count = count($patternList);
        $sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
        $replaceArray = array_combine($patternList,array_fill(0,count($patternList),'*')); //把匹配到的数组进行合并,替换使用
        $stringAfter = strtr($string, $replaceArray); //结果替换
    }
    $log = "原句为 [ {$string} ]
"; if($count==0){ $log .= "暂未匹配到敏感词!"; }else{ $log .= "匹配到 [ {$count} ]个敏感词:[ {$sensitiveWord} ]
". "替换后为:[ {$stringAfter} ]"; } return $log; } function testAction(){ $string = 'likeyou小白喜欢小黑爱着的大黄'; //要过滤的内容 $list = ['小明', '小红', '大白', '小白', '小黑', 'me', 'you']; //定义敏感词数组 $result = sensitive($list, $string); echo ($result); die; }

方法三:

 $v ){
            self::addKeyWord($v['name']);
        }
        return file_put_contents(self::$file,serialize(self::$arrHashMap));

    }

    /**
     * 过滤敏感词
     * @param $strWord
     * @return mixed
     */
    public static function filterSensitiveWord( $strWord ){
        $file = unserialize(file_get_contents(self::$file));
        $resStr  = $strWord;
        if(!empty($file)){
            $len = mb_strlen($strWord, 'UTF-8');
            $arrHashMap = self::$arrHashMap = $file;
            $newWord = '';
            for ($i=0; $i < $len; $i++) {
                $word = mb_substr($strWord, $i, 1, 'UTF-8');
                if (!isset($arrHashMap[$word])) {
                    $arrHashMap = self::$arrHashMap;
                    $newWord = '';
                }
                $newWord .= $word;
                if ($arrHashMap[$word]['end']) {
                    $asterisk = self::getAsterisk(mb_strlen($newWord, 'UTF-8'));
                    $resStr = str_replace($newWord,$asterisk,$resStr);
                    $newWord = '';
                    $arrHashMap = self::$arrHashMap;
                } else{
                    $arrHashMap = $arrHashMap[$word];

                }
            }
        }

        return $resStr;
    }

    /**
     * 过滤邮箱和手机号(8位以上数字)
     * @param $msg
     * @return string
     */
    public static function filterTelMail( $msg ):string {
        if(is_string((string)$msg)){
            $msg = preg_replace('/\d{8,}/', '****', $msg);
            $msg = preg_replace('/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})/i', '****', $msg);
        }else{
            $msg = '';
        }

        return $msg;
    }
    /**
     * 新增敏感词的核心方法
     * @param $strWord
     */
    private static function addKeyWord( $strWord ) { //免定金峨眉牌汽枪
        $len = mb_strlen($strWord, 'UTF-8');

        $arrHashMap = &self::$arrHashMap;
        for ($i=0; $i < $len; $i++) {
            $word = mb_substr($strWord, $i, 1, 'UTF-8');
            // 已存在
            if (isset($arrHashMap[$word])) {
                if ($i == ($len - 1)) {
                    $arrHashMap[$word]['end'] = 1;
                }
            } else {
                // 不存在
                if ($i == ($len - 1)) {
                    $arrHashMap[$word] = [];
                    $arrHashMap[$word]['end'] = 1;
                } else {
                    $arrHashMap[$word] = [];
                    $arrHashMap[$word]['end'] = 0;
                }
            }
            // 传址
            $arrHashMap = &$arrHashMap[$word];
        }
    }

    /**
     * 生成*号
     * @param int $num
     * @return string
     */
    private static function getAsterisk( int $num ) :string {
        $str = '';
        for($i=1;$i<=$num;$i++) {
            $str .= '*';
        }
        return $str;
    }

}

 

你可能感兴趣的:(php)