zoukankan      html  css  js  c++  java
  • 过滤替换文章内的敏感词或字

            //接收关键词
            $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' => "评论中无敏感词或字"]);
            }
        }
  • 相关阅读:
    tomcat配置数据源
    Spring 配置详解
    典型的软件开发模型
    600字让你读懂Git
    JVM的自愈能力
    Maven的pom.xml文件详解
    如何使用Log4j
    掌握jQuery插件开发,这篇文章就够了
    CSS Gradient详解
    CSS Transition
  • 原文地址:https://www.cnblogs.com/kissmy/p/10450004.html
Copyright © 2011-2022 走看看