本文目錄一覽:
php中如何用空格分隔字元串,如 str = ‘好好學習’;分割後效果str=’好 好 學 習’;
/**
* 分割字元串
* $str : 要分割的字元串
* $cut_len : 間隔
* $f : 分割的字元
*/
function cut_string($str,$cut_len, $f = ‘ ‘){
$len = mb_strlen($str,’utf-8′);//獲取字元串長度
$content = ”;
for($i=0;$iceil($len/$cut_len);$i++){
$content .= mb_substr($str,$cut_len*$i,$cut_len,’utf-8′).$f;//遍歷添加分隔符
}
$content = trim($content,$f);//去除字元串中最後一個分隔符
return $content;
}
echo cut_string(‘好好學習’, 1);
這樣就可以了。
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語言中字元串分割用什麼函數?
「php分割字元串的函數有explode()和str_split() explode()」【摘要】
php語言中字元串分割用什麼函數?【提問】
「php分割字元串的函數有explode()和str_split() explode()」【回答】
explode() 函數使用一個字元串分割另一個字元串,並返回由字元串組成的數組。【回答】
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/311419.html