php正则表达式 剔除字符串中的除了汉字的字符(只保留汉字)

1)utf-8的编码格式,匹配中文代码如下:

<?php
    $str = "utf-8下匹配出中文字符串";
    $preg = "/[\x{4e00}-\x{9fa5}]+/u";
    if(preg_match_all($preg,$str,$matches)){
        print_r($matches);
    }
?>

2)gb2312的编码格式,匹配中文字符串代码如下:

<?php
    $str = "gb2312下匹配出中文字符串";
    $preg = "/([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])+/i";
    if(preg_match($preg,$str,$matches)){
        print_r($matches);
    }
?>

--------------------- 本文来自 a771948524 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zxlstudio/article/details/26575599?utm_source=copy

转载 https://blog.csdn.net/a913518111/article/details/82866521

 

            $str = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $v); //"甜蜜世家"
            //$v2 = "3东里屯250";
            $result = explode('-',preg_replace('/[\x{4e00}-\x{9fa5}]+/u','-',$v));
            $jl_order_num = $result[1];//商品数量
            $jl_market_name = $str;//超市名称

 

---------------------------------------------------------------------------------

 

php - 中文字符串分割


//先删除掉非中文的字体
$str = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $str);
//经过测试中文占3个篇幅
$re = chunk_split($str,3,",");
//再利用explode将字符串分割为数组 $re = explode(",",$re);


转载 https://www.cnblogs.com/CyLee/p/5552668.html

 

posted @ 2020-05-29 11:53  搬砖小伙子  阅读(1487)  评论(0编辑  收藏  举报