本文目錄一覽:
- 1、php分割字元串
- 2、php 分割字元串
- 3、php拆分字元串
- 4、php語言中字元串分割用什麼函數?
php分割字元串
/**
* 寬字元串分割
*
* @param unknown_type $str
* @return unknown
*/
function split ($str) {
$ret = array();
$len = strlen($str);
for ($i = 0; $i $len; $i ++) {
// 判斷編碼位數
$code = ord($str[$i]);
// 單位元組 0
if ($code 7 == 0) {
$ret[] = $str[$i];
}
// 4 位元組 1111
else if ($code 4 == 15) {
if ($i $len – 3) {
$ret[] = $str[$i] . $str[$i + 1] . $str[$i + 2] + $str[$i + 3];
$i += 3;
}
}
// 3 位元組 111
else if ($code 5 == 7) {
if ($i $len – 2) {
$ret[] = $str[$i] . $str[$i + 1] . $str[$i + 2];
$i += 2;
}
}
// 雙位元組 11
else if ($code 6 == 3) {
if ($i $len – 1) {
$ret[] = $str[$i] . $str[$i + 1];
$i += 1;
}
}
}
return $ret;
}
上面是分隔中文字串為數組的.
這種方式性能比正則表達式要高點. GBK , UTF-8 編碼都是支持的.
分隔完畢之後, 你在循環數組, 設置步長為3 . 然後就可以拼接出你要的了.
php 分割字元串
這100分來得好爽哦,樓上的正解。
關於str_split的詳細例子程序:
?php
$str = “Hello Friend”;
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?
上例將輸出:
Array
(
[0] = H
[1] = e
[2] = l
[3] = l
[4] = o
[5] =
[6] = F
[7] = r
[8] = i
[9] = e
[10] = n
[11] = d
)
Array
(
[0] = Hel
[1] = lo
[2] = Fri
[3] = end
)
但是,str_split不支持漢字,會把漢字分為兩半,需要把漢字當為一個字元進行處理的時候,需要自己編寫函數。
php拆分字元串
可以用正則和字元串分詞~,下面這個是按照逗號或空格分詞~
$str
=
‘豆瓣,人人,開心’;
$str4
=
str_replace(‘,’,’,’,$str);
//將中文逗號轉換成英文逗號,很重要
$key
=
preg_split(‘/[\s,]+/’,$str4);
//分詞功能
foreach($key
as
$value){
echo
$value;
echo
‘
‘;
}
php語言中字元串分割用什麼函數?
「php分割字元串的函數有explode()和str_split() explode()」【摘要】
php語言中字元串分割用什麼函數?【提問】
「php分割字元串的函數有explode()和str_split() explode()」【回答】
explode() 函數使用一個字元串分割另一個字元串,並返回由字元串組成的數組。【回答】
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/243314.html