过滤违禁词

admin1年前Laravel587

/**
* 内容验证
*/
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;
}

相关文章

抖音小程序码

public function dyqrcode($uid){    $url = "https://open.douyin.com/api/apps/v1/qrcode...

阿里云发送短信

public function sms(){    $moile = request()->input('mobile');    if...

获取微信的Token

 public function Token()    {        $this->path = __DIR__ . &...

微信退款v3

/** * 微信 退款 操作 */ public function refund($out_trade_no, $refund_money) { $time = time();...

关注公众号并登录

/* * 服务端生成带唯一标识二维码并将唯一标识返回给前端 */ public function wx_code() { $scene_str = $this->get_...

laravel 加cache锁

use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Cache; $lock = Ca...