導航:首頁 > 編程語言 > php中文字元串分割

php中文字元串分割

發布時間:2023-01-15 06:45:47

php分割中英文字元串的幾種方

對一段文字按照字數進行分割,因為文字中可能是中英文混合的,而php函數strlen只能計算出字串的位元組數,於是自己實現了幾個函數,分享下。
例1,計算字元總長度。
01<?php
02functionccStrLen($str)#計算中英文混合<ahref="/"target="_blank"class="infotextkey">字元串</a>的長度
03{
04$ccLen=0;
05$ascLen=strlen($str);
06$ind=0;
07$hasCC=ereg(」[xA1-xFE]「,$str);#判斷是否有漢字
08$hasAsc=ereg(」[x01-xA0]「,$str);#判斷是否有ASCII字元
09if($hasCC&&!$hasAsc)#只有漢字的情況
10returnstrlen($str)/2;
11if(!$hasCC&&$hasAsc)#只有Ascii字元的情況
12returnstrlen($str);
13for($ind=0;$ind<$ascLen;$ind++)
14{
15if(ord(substr($str,$ind,1))>0xa0)
16{
17$ccLen++;
18$ind++;
19}
20else
21{
22$ccLen++;
23}
24}
25return$ccLen;
26}
27?>
例2,從左側截取字元串。
01<?php
02functionccStrLeft($str,$len)#從左邊截取中英文混合字元串
03{
04$ascLen=strlen($str);if($ascLen<=$len)return$str;
05$hasCC=ereg(」[xA1-xFE]「,$str);#同上
06$hasAsc=ereg(」[x01-xA0]「,$str);
07if(!$hasCC)returnsubstr($str,0,$len);
08if(!$hasAsc)
09if($len&0×01)#如果長度是奇數
10returnsubstr($str,0,$len+$len-2);
11else
12returnsubstr($str,0,$len+$len);
13$cind=0;$flag=0;$reallen=0;//實際取位元組長
14while($cind<$ascLen&&$reallen<$len)
15{//bywww.jbxue.com
16if(ord(substr($str,$cind,1))<0xA1){//如果該位元組為英文則加一
17$cind++;
18}else{//否則加2個位元組
19$cind+=2;
20}
21$reallen++;
22}
23returnsubstr($str,0,$cind);
24}
25?>
例3,把給定文字,按切割數量存入數組(適合短篇文字,長文章可沒分隔一部分就直接處理一次)
viewsourceprint?
01<?php
02functionSplitContent($content,$smslen){
03$str_tmp=$content;
04$arr_cont=array();
05$len_tmp=0;
06$i=0;//分割絕對位置
07while(strlen($str_tmp)>0){
08$str_tmp=ccStrLeft($str_tmp,$smslen);
09array_push($arr_cont,$str_tmp);
10$i+=strlen($str_tmp);
11$str_tmp=substr($content,$i,strlen($content));
12}
13return$arr_cont;
14}//bywww.jbxue.com
15?>
測試:
1<?php
2$str=』a計算中英文混合1234字元串的長度abcd』;
3echo$str.』的長度為:』.ccStrLen($str);
4echo『<br>』;
5$smslen=3;//截取長度
6print_r(SplitContent($str,$smslen));
7?>

❷ php分割中文字元串

$str = ereg_replace('[\W\_]+','',$str)

\W 大寫W

===================================

噢。。。你要的是保留中文呀。。。

$str = ereg_replace('[\w[:punct:]]+','',$str)

❸ PHP中如何分割字元串

最常用函數 substr(); 不考慮性能情況下的,萬能分割函數
$str = "hello";
substr($str,1); // ello
substr($str,0,3); // hel
substr($str,3,1); // l
不用我再解釋吧

❹ php怎麼把字元串指定字元分割成數組

x0dx0a$var=explode("|",$str);x0dx0a把$str按|進行分割x0dx0aphp還有其他的把字元串指定字元分割成數組x0dx0a str_split(string,length)參數 描述 x0dx0astring 必需。規定要分割的字元串。 x0dx0alength 可選。規定每個數組元素的長度。默認是 1。 x0dx0a json_decode()這個函數也可以把字元串分割成數組 (第二個參數為true才行)

❺ 怎樣將php里的漢字字元串分割成單個字元

如果你的字串是固定的,就先用substr() 固定截圖來賦值把,(漢子截取2個位元組)

$str = "怎麼chai數組123";

function str_arr($str){
$str1 = substr($str,0,1);
echo $str1;
$e = preg_match("/^[\x7f-\xff]+$/",$str1);
echo $e;
if($e){$ = "y"; }else{$ = 'n';}
echo $;
$new = array();
if($e){
$new[] = $str1;
$str_r = substr($str,2);
}else{
$str2 = substr($str,0,2);
$new[] = $str2;
$str_r = substr($str,2);
}

return $new;
}
$ss = str_arr($str);

var_mp($ss);

❻ php語言中字元串分割用什麼函數

「php分割字元串的函數有explode()和str_split() explode()」【摘要】
php語言中字元串分割用什麼函數?【提問】
「php分割字元串的函數有explode()和str_split() explode()」【回答】
explode() 函數使用一個字元串分割另一個字元串,並返回由字元串組成的數組。【回答】

❼ 「PHP基礎」字元串分割 explode 與 str_split 函數

用於分割字元串。

相關函數如下:

本函數為 implode() 的反函數,使用一個字元串分割另一個字元串,返回一個數組。

語法:

array explode( string separator, string string [, int limit] )

例子:

輸出結果如下:

str_split() 將字元串分割為一個數組,成功返回一個數組。

語法:

array str_split( string string [, int length] )

例子:

輸出結果如下:

閱讀全文

與php中文字元串分割相關的資料

熱點內容
雲主機伺服器購買配置 瀏覽:201
虛擬伺服器有什麼優點 瀏覽:615
devcpp文件夾可以刪除嗎 瀏覽:97
實習律師如何使用個稅app 瀏覽:839
伺服器如何對抗管理員 瀏覽:386
程序員帶辣條參奧運 瀏覽:16
程序員月薪5萬元 瀏覽:404
最優選擇演算法 瀏覽:901
空調壓縮機一直工作 瀏覽:979
phpinstallmbstring 瀏覽:18
hidpdf 瀏覽:54
電腦自動彈出小窗口發送命令 瀏覽:822
激活碼登錄功能php源碼 瀏覽:331
世醫得效方pdf 瀏覽:275
手機游戲反編譯要改哪些文件 瀏覽:828
海底珊瑚網解壓 瀏覽:64
蘋果手機不帶應用加密嗎 瀏覽:5
軟體加密綜合應用前景 瀏覽:584
程序員學霸說脫口秀 瀏覽:803
java導出數據XML 瀏覽:174