str_split() 函数无法分隔中文字符的问题,可以使用以下函数替代:
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; } print_r(mb_str_split('RUNOOB')); print_r(mb_str_split('菜鸟教程'));
以上代码执行结果为:
Array ( [0] => R [1] => U [2] => N [3] => O [4] => O [5] => B ) Array ( [0] => 菜 [1] => 鸟 [2] => 教 [3] => 程 )