Write your own php by string length to split the code, supporting Chinese characters, the following code and the use of methods, the need for small partners can refer to.
Here is the code I wrote to split the Chinese and English mixed characters:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php 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 ' <pre> '; Print_r ($R); Echo ' </pre> ';?> |
Run Result:
?
| 1 2 3 4 5 6 7 8 9 |
Array ([0] => I love Beijing [1] => 3 I fell in love [2] => Sea-I love [3] => Xian [4] => Ggan [5] => g) |
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 |
<?php function Str_split_unicode ($str, $l = 0) {if ($l > 0) {$ret = array (); $len = Mb_strlen ($str, "UTF-8"); $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 = "have not yet SSS see GFG not Rtret Immortal"; Mild Milk Print_r (Str_split ($s, 5)); Print_r (Str_split_unicode ($s, 5)); Output//array ([0] => also [1] => as [2] => SSS [3] => See GF [4] => G [5] => RTR [6] => et not [7] = > Rot)//array ([0] => has not yet fulfilled s [1] => SS See GF [2] => g non RT [3] => ret Immortal) |
The above mentioned is the entire content of this article, I hope you can enjoy.