PHP support for Chinese string segmentation functions
This article to you to share 2 PHP using mb_xxx method to achieve the Chinese character segmentation method, the basic ideas are similar, the need for small partners can refer to.
Str_split does not support Chinese and uses MB_XX functions 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 */ function Mb_str_split ($str, $split _length=1, $charset = "UTF-8") { if (Func_num_args () ==1) { return Preg_split ('/(? } 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 whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1007646.html www.bkjia.com true http://www.bkjia.com/PHPjc/1007646.html techarticle PHP Support Chinese string segmentation function This article to share the 2 PHP using the Mb_xxx method to achieve Chinese character segmentation method, the basic ideas are similar, the need for a small partner can ...