Php string and Chinese Character splitting method-PHP source code

Source: Internet
Author: User
Ec (2); if you use the php tutorial function & ldquo; str_split & rdquo; to separate it, garbled characters will appear, because the length of Chinese characters is different from that of English characters. However, we can create a new function to convert characters into ascii values first, then correctly split the Chinese strings by judging the length of different characters, store the results into an array, and then use the php function & ldquo; join & rdquo; insert percent sign between characters & lt ;? Phpfunctionstr_split_ut script ec (2); script

If you use the php tutorial function "str_split" to separate the strings, garbled characters will appear, because the length of Chinese characters is different from that of English characters. However, we can create a new function to convert characters into ascii values first, and then divide the Chinese strings correctly by judging the length of different characters, and store the results into arrays, finally, use the php function "join" to insert a percent sign between characters.

Function str_split_utf8 ($ str ){
$ Split = 1;
$ Array = array ();
For ($ I = 0; $ I $ Value = ord ($ str [$ I]);
If ($ value & gt; 127 ){
If ($ value >=192 & $ value <= 223) $ split = 2;
Elseif ($ value >=224 & $ value <= 239) $ split = 3;
Elseif ($ value >=240 & $ value <= 247) $ split = 4;
} Else {
$ Split = 1;
}
$ Key = null;
For ($ j = 0; $ j <$ split; $ j ++, $ I ++ ){
$ Key. = $ str [$ I];
}
Array_push ($ array, $ key );
}
Return $ array;
}
$ String = "www.111cn.net ";
$ Arr1 = str_split_utf8 ($ string );
Echo join ("%", $ arr1 );
?>

Method 2

$ Str = "a poly tutorial Network: http://www.111cn.net ";
Function mbstringtoarray ($ str, $ charset ){
$ Strlen = mb_strlen ($ str );
While ($ strlen ){
$ Array [] = mb_substr ($ str, 0, 1, $ charset );
$ Str = mb_substr ($ str, 1, $ strlen, $ charset );
$ Strlen = mb_strlen ($ str );
}
Return $ array;
}
$ Arr = mbstringtoarray ($ str, "gb2312 ");
?>


Note:

1. The $ charset variable is webpage encoding, for example, "gb2312" or "UTF-8 ";

2. method 1 requires that the server must enable the mbstring. dll extension. Otherwise, the code execution is incorrect. Therefore, you can consider using the second method for your friends who use the VM.

Method 2:

Function str_to_arr ($ str ){
$ L = strlen ($ str );
For ($ I = 0; $ I <$ l; $ I ++ ){
$ Arr [] = ord ($ str [$ I]) & gt; 127? $ Str [$ I]. $ str [++ $ I]: $ str [$ I];
}
Return $ arr;
}
$ Arr = str_to_arr ($ str );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.