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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM