PHP that supports Chinese characters is split into array code by String Length
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:
?
|
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> ';
?>
|
Running 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
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 ");
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 = "sss not expected 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] => �� [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.