|
@@ -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);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $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['string'] = $string;
|
|
|
+ $rs['count'] = $count;
|
|
|
+ $rs['keyworld'] = $patternList;
|
|
|
+ $rs['newstring'] = $stringAfter;*/
|
|
|
+
|
|
|
+ return $stringAfter;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function checkAction($data,$fields){
|
|
|
+
|
|
|
+ foreach($data as $key => $string){
|
|
|
+ if(in_array($key,$fields)){
|
|
|
+ $data[$key] = self::sensitive($string);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+}
|