过滤违禁词

admin8个月前Laravel425

/**
* 内容验证
*/
public function check($content)
{
   $count = 0; //违规词的个数
   $list = $this->datasens();  //定义敏感词数组
   $pattern = "/" . implode("|", $list) . "/i"; //定义正则表达式
   if (preg_match_all($pattern, $content, $matches)) { //匹配到了结果
       $patternList = $matches[0];  //匹配到的数组
       $count = count($patternList);
       $sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
       $replaceArray = array_combine($patternList, array_fill(0, count($patternList), '*')); //把匹配到的数组进行合并,替换使用
       $stringAfter = strtr($string, $replaceArray); //结果替换
   }
   if ($count > 0) {
       return false;
   }
   return true;
}


/**
* 过滤敏感词
*/
public function datasens()
{

   if (Cache::get('min')) {
       $data = Cache::get('min');
   } else {
       $array = [];
       $list = Db::table('sens')->where([])->get(['name']);
       foreach ($list as $k => $v) {
           $array[] = $v->name;
       }
       Cache::put('min', $array);
       $data = $array;
   }
   return $data;
}

相关文章

手机号替换

 $vv = substr_replace($vv, '****', 3, 4) ....

压缩图片

public static function size($id, $img) { try { list($width, $height, $img_type, $attr) = @getimages...

php 优化

使用 gc_collect_cycles() 强制进行垃圾回收 PHP 的垃圾回收机制会自动释放不再使用的变量所占用的内存。然而,在某些情况下,尤其是在长时间运行的脚本(如 CLI...

修改密码

public function setpass(){    $mobile = request()->input('mobile');   &nbs...

base64 处理图片

public function images(){    $image = request()->input('image');    i...

忘记密码

publicfunction forget(){    $phone = request()->input('phone');    $c...