• 过滤替换文章内的敏感词或字


            //接收关键词
            $str = input('words');
            $txt = ComType::field('title,times,id')->select();
            //得到所有的过滤词
            if (count($txt) > 0) {
                foreach ($txt as $w) {
                    $k[] = $w['title'];
                }
            }
            //获取数组
            foreach ($k as $key => $val) {
                //去空格
                $val = trim($val);
                //正则过滤
                preg_match('/' . $val . '/', $str, $matches);
                //替换掉敏感词
                if (!empty($matches[0])) {
                    $str =  str_replace($val, "***", $str);
                }
                //收集所有的敏感词
                $m[] = $matches;
            };
            //判断是否存在敏感词
            $r = strpos($str, '*');
            if ($r != false || $r == 0) {
                //统计匹配次数
                if ($m) {
                    foreach ($m as $i) {
                        if ($i) {
                            $t = ComType::where('title', $i[0])->find();
                            if ($t) {
                                ComType::where('id', $t['id'])->update(['times' => $t['times'] + 1]);
                            }
                        }
                    }
                }
                return json(['code' => 1, 'data' => $str, 'msg' => "评论中含有敏感字或词"]);
            } else {
    
                return json(['code' => 2, 'data' => '', 'msg' => "评论中无敏感词或字"]);
            }
        }
  • 相关阅读:
    新的学期,新的学习
    织梦最新
    selectd选项跳转链接
    字体统一大小
    js点击后进入到另一个页面的TAB选项中
    织梦套后台
    js获取距离各边的距离
    将时间的秒数转化为分钟数
    jquery手写tab选项卡功能
    js中的定时器
  • 原文地址:https://www.cnblogs.com/kissmy/p/10450004.html
Copyright © 2020-2023  润新知