PHP compiled by myself is divided into array code by string length. it supports Chinese characters. the following code and usage are provided. if you need it, refer to it. The following code is used to separate Chinese and English characters:
<? Phpfunction 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 '
';?>
Running result:
Array ([0] => I love Beijing [1] => 3 I love [2] => sea-I love [3] => xian [4] => ggan [5] => g)
<? 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 = "if you do not want sss to see gfg do not rtret immortal"; // Mild milk print_r (str_split ($ s, 5); print_r (str_split_unicode ($ s, 5 )); // output // Array ([0] => [1] => �� for example, [2] => �� sss [3] => see gf [4] => g users [5] => �� rtr [6] => et does not [7] => bytes) // Array ([0] => s [1] => ss see gf [2] => g do not rt [3] => ret immortal)
The above is all the content of this article. I hope you will like it.