This article to share 2 PHP using mb_xxx method to achieve the Chinese character segmentation method, its basic ideas are similar, the need for small partners can refer to.
Str_split does not support Chinese and uses the MB_XX function to implement a
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * Convert A string to an array * @param string $str * @param number $split _length * @return multitype:string * * functi On 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; } |
Method Two:
?
| 1 2 3 4 5 6 7 8 9 10 |
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; } |
The above mentioned is the entire content of this article, I hope you can enjoy.