博主信息
博文 15
粉丝 0
评论 0
访问量 24586
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP中文替换
李子的博客
原创
2647人浏览过

<?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>";

        

    }

1、匹配中文

$str = "中文“;
preg_match_all("/[\x{4e00}-\x{9fa5}]+/u",$str,$match);

 

2、替换中文:

在所在的php文件里,要加上

mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");

这样才能支持多字节进行模式匹配。详细介绍:http://blog.chinaunix.net/uid-20279807-id-1711213.html

3、php提供了四个替换函数,分别是str_replace,preg_replace,mb_ereg_replace,ereg_replace(在php7.1已经摒弃掉)

   在替换中文时,发现用preg_replace替换中文最合适.

   str_replace 不支持正则表达式,不能完全匹配,导致局部字段被替换。例如: $str = "模块一 模块一断电",$str = str_replace("模块一","module1",$str);,导致"模块一断电"被替换成"module1断电"。

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )  支持$pattern,$replacement 以数组的方式进行查找替换,但数组过多时,进行搜索匹配,耗CPU严重。

 mb_ereg_replace 支持正则表达式,但不用分隔符//进行匹配,但使用mb_ereg_replace,发现有些中文匹配不了。具体原因暂不清楚。


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学