php查找/过滤一段文字中的违禁词敏感词

2018-01-20编程教程
276

<?php
   //定义编码 
  header( 'Content-Type:text/html;charset=utf-8 '); 
   
   $words=array('我','你','他');
   $content="测一测我是不是违禁词";
   $banned=generateRegularExpression($words);
  //检查违禁词
   $res_banned=check_words($banned,$content);
   write_html($content,$res_banned);
   
  /**
     * @describe 数组生成正则表达式
     * @param array $words
     * @return string
     */
    function generateRegularExpression($words)
    {
        $regular = implode('|', array_map('preg_quote', $words));
        return "/$regular/i";
    }
    /**
     * @describe 字符串 生成正则表达式
     * @param array $words
     * @return string
     */
    function generateRegularExpressionString($string){
          $str_arr[0]=$string;
          $str_new_arr=  array_map('preg_quote', $str_arr);
          return $str_new_arr[0];
    }
    /**
     * 检查敏感词
     * @param $banned
     * @param $string
     * @return bool|string
     */
    function check_words($banned,$string)
    {    $match_banned=array();
        //循环查出所有敏感词
 
        $new_banned=strtolower($banned);
        $i=0;
        do{
            $matches=null;
            if (!empty($new_banned) && preg_match($new_banned, $string, $matches)) {
                $isempyt=empty($matches[0]);
                if(!$isempyt){
                    $match_banned = array_merge($match_banned, $matches);
                    $matches_str=strtolower(generateRegularExpressionString($matches[0]));
                    $new_banned=str_replace("|".$matches_str."|","|",$new_banned);
                    $new_banned=str_replace("/".$matches_str."|","/",$new_banned);
                    $new_banned=str_replace("|".$matches_str."/","/",$new_banned);
                }
            }
            $i++;
            if($i>20){
                $isempyt=true;
                break;
            }
        }while(count($matches)>0 && !$isempyt);
 
        //查出敏感词
        if($match_banned){
            return $match_banned;
        }
        //没有查出敏感词
        return array();
    }
     
      /**
     * 打印到页面上
     * @param $filepath
     * @param $res_mingan
     * @param $res_banned
     */
    function write_html($content,$res_banned){
       
            print_r($content);
            if($res_banned){
                print_r("  <font color='red'>违禁词(".count($res_banned)."):</font>".implode('|',$res_banned));
            }
            echo "<br>";
        
    }

The End

相关推荐

layui根据百度地图经纬度在弹出层中显示位置
首先你需要引入百度地图的js script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0ak=你的ak"/script ak ,注意是要浏览器端的ak,这个直接到百度开发者平台申请。 引入layui,这个大家可以到layui官网看看怎么引入layer, 注意:这里要提...
2025-01-09 编程教程
240

百度UEditor编辑器如何禁止过滤div等网页html标签
将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签。 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找到如下的代码,将true设置为false me.setOpt({ 'allowDivTransToP':false, 'disabledTableInTable'...
2022-11-23 编程教程
495

layui Table 设置title 字体加粗
在layui.css中加样式 : .layui-table th{font-weight: bold;} ,或者直接加在网页中即可。 style .layui-table th{ font-weight: bold;} /stylebodytable id="demo" lay-filter="demo" class="layui-hide"/table/body...
2022-10-17 编程教程
200

PHP错误Warning: Cannot modify header information - headers alr
今天在用php进行图片保存输出时候,图片一直显示错误,后面用调试模式下提示:Warning: Cannot modify header information - headers already sent by... 看了一些网上的方法也没解决,最后在php.ini配置output_buffering默认为4096就没有遇到这个错误了: o...
2022-05-18 编程教程
69

Parse error: syntax error, unexpected '&'解决办法
在使用PHP5.4及以上版本时,在调用函数时,使用引用符号时,会出现Parse error: syntax error, unexpected 或PHP Fatal error: Call-time pass-by-reference has been removed者,这是由于在函数调用时通过引用传递参数已被弃用,因为它影响了代码的整洁,如...
2022-05-11 编程教程
295

解析PHP中ob_start()函数的用法
ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车/空格/换行/都会有Header had all ready send by的错误,这时可以先用ob_start()打开缓冲区PHP代码的数据块和echo()输出都会进入缓冲区而不会立刻输出.当然打开缓冲区的作用很多,只要...
2022-05-11 编程教程
106