PHP-分割字符串(支持中文)
第一种:
代码语言:javascript复制function mb_str_split($str,$split_length=1,$charset="UTF-8"){
if(func_num_args()==1){
return preg_split('/(?<!^)(?!$)/u', $str);
}
if($split_length<1)return false;
$len = mb_strlen($str, $charset);
$arr = array();
for($i=0;$i<$len;$i =$split_length){
$s = mb_substr($str, $i, $split_length, $charset);
$arr[] = $s;
}
return $arr;
}
第二种
代码语言:javascript复制function mbStrSplit ($string, $len=1) {
$start = 0;
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,$start,$len,"utf8");
$string = mb_substr($string, $len, $strlen,"utf8");
$strlen = mb_strlen($string);
}
return $array;
}
本文采用 「CC BY-NC-SA 4.0」创作共享协议,转载请标注以下信息:
原文出处:Yiiven https://cloud.tencent.com/developer/article/2193253