php支持中文字符串分割的函数

str_split不支持中文,利用mb_xx函数实现个

/** * Convert a string to an array * @param string $str * @param number $split_length * @return multitype:string */ 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; }

方法二:

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; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0dc65417b33d80a38123d0da113eca91.html