Chinese-supported PHP splits the array code by string length
PHP's own written by the length of the string to split the array of code, support Chinese characters, the following code and use the method, the need for small partners can refer to.
Here is the code I wrote to enable the segmentation of Chinese and English mixed characters:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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; } Header (' Content-type:text/html;charset=utf-8 '); $str = ' I love Beijing 3 I love Shanghai-I love Xianggang '; $r = Mbstrsplit ($str, 4); Echo ' '; Print_r ($R); Echo ' ';?> |
Operation Result:
?
1 2 3 4 5 6 7 8 9 |
Array ( [0] = i love Beijing [1] = 3 I love [2] = sea-I love [3] = Xian [4] = Ggan [5] = g ) |
?
| 1 2 3 4 5 6 7 8 9 Ten + + / / / |
!--? php Function str _split_unicode ($str, $l = 0) { if ($l > 0) { $ret = array (); $len = Mb_strlen ($str, "UTF-8"); for ($i = 0; $i < $len; $i + = $l) { $ret [] = Mb_substr ($str, $i, $l, "UTF-8"); } Return $ret; } Return Preg_split ("//u", $str,-1, preg_split_no_empty); } ? $s = "the GFG is not Rtret immortal";//Mild Milk Print_r (Str_split ($s, 5)); Print_r (Str_split_unicode ($s, 5)); //Output //array ([0] = [1] = = [2] = SSS [3] = See GF [4] = g [5] = RTR [6] = et not [7] = decay) //array ([0] = = Not yet desired s [1] = SS See GF [2] = g no RT [3] = ret Immortal) |
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1000068.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000068.html techarticle support for Chinese PHP by the length of the string to split the number of code to write their own PHP by the length of the string to split the number of code, support Chinese characters, the following code and the use of methods, there is a need ...