Browse Source

关键字过滤,实操方法

lizhen_gitee 3 years ago
parent
commit
b0500832bd
2 changed files with 70 additions and 1 deletions
  1. 11 1
      application/api/controller/Userdongtai.php
  2. 59 0
      application/common/library/Keyworld.php

+ 11 - 1
application/api/controller/Userdongtai.php

@@ -4,12 +4,13 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use think\Db;
+use app\common\library\Keyworld;
 /**
  * 普通动态
  */
 class Userdongtai extends Api
 {
-    protected $noNeedLogin = ['info','floor_info'];
+    protected $noNeedLogin = ['info','floor_info','test'];
     protected $noNeedRight = ['*'];
 
 
@@ -35,6 +36,12 @@ class Userdongtai extends Api
         $this->success('success',$list);
     }
 
+    public function test(){
+        $content = '你习近平哈阿第三方毛泽东哈共产AA';
+        $content = Keyworld::sensitive($content);
+        dump($content);
+    }
+
     //发布动态
     public function addone(){
         $content = input('content','');
@@ -43,6 +50,9 @@ class Userdongtai extends Api
             $this->error(__('Invalid parameters'));
         }
 
+        //关键字替换
+        $content = Keyworld::sensitive($content);
+
         $data = [
             'user_id' => $this->auth->id,
             'content' => $content,

+ 59 - 0
application/common/library/Keyworld.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace app\common\library;
+
+use think\Hook;
+use think\Config;
+/**
+ * 关键词检索类
+ */
+class Keyworld
+{
+
+    /**
+     * @todo 敏感词过滤,返回结果
+     * @param array $list  定义敏感词一维数组
+     * @param string $string 要过滤的内容
+     * @return string $log 处理结果
+     */
+    public static function sensitive($string){
+        $count = 0; //违规词的个数
+        $sensitiveWord = '';  //违规词
+        $stringAfter = $string;  //替换后的内容
+        $list = config('keyworld');  //定义敏感词数组
+
+        $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),'*')); //把匹配到的数组进行合并,替换使用
+            $replace_arr = [1=>'*',2=>'**',3=>'***',4=>'****',5=>'*****','more'=>'*****...',];
+            $replaceArray = [];
+            foreach($patternList as $key => $val){
+                $replaceArray[$val] = isset($replace_arr[mb_strlen($val)]) ? $replace_arr[mb_strlen($val)] : $replace_arr['more'];
+            }
+            $stringAfter = strtr($string, $replaceArray); //结果替换
+        }
+
+        /*$rs = [];
+        $rs['string'] = $string;
+        $rs['count'] = $count;
+        $rs['keyworld'] = $patternList;
+        $rs['newstring'] = $stringAfter;*/
+
+        return $stringAfter;
+    }
+
+    public static function checkAction($data,$fields){
+        //$string = 'likeyou小白喜欢小黑爱着的大黄'; //要过滤的内容
+        foreach($data as $key => $string){
+            if(in_array($key,$fields)){
+                $data[$key] = self::sensitive($string);
+            }
+        }
+
+        return $data;
+    }
+}