Determine whether there are any Chinese characters in the content, and then return the Chinese characters first, I use preg_match_all ([x80-xff]., & nbsp; $ s, $ arr); check whether there are any Chinese characters, and then print $ arr to check what are Chinese characters and modify them accordingly, but print $ arr is garbled, using several GB2312 to UTF-8 method can not. You can help provide some methods. Determine whether there are any Chinese characters in the content, and then return Chinese characters
First, I use preg_match_all ('/[\ x80-\ xff]. /', $ s, $ arr); check whether there are any Chinese characters, and then print $ arr to check what are Chinese characters and modify them accordingly, but printing $ arr is garbled, using several GB2312 to UTF-8 method can not.
You can help provide some methods.
------ Solution --------------------
1. UTF-8 is encoded as follows:
U + 007F 0 xxxxxxx
U + 07FF 110 xxxxx 10 xxxxxx
U + FFFF 1110 xxxx 10 xxxxxx 10 xxxxxx
U + 1 FFFFF 11110xxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
U + 3 FFFFFF 111110xx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
U + 7 FFFFFFF 1111110x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
2. we can see that the regular expression string in Chinese is
/^ (? : [\ X00-\ x7f]
------ Solution --------------------
[\ Xc0-\ xff] [\ x80-\ xbf] +) + $/
3. this function is provided in the appendix of the manual.
Function is_utf8 ($ str ){
$ C = 0; $ B = 0;
$ Bits = 0;
$ Len = strlen ($ str );
For ($ I = 0; $ I <$ len; $ I ++ ){
$ C = ord ($ str [$ I]);
If ($ c & gt; 128 ){
If ($ c> = 254) return false;
Elseif ($ c >=252) $ bits = 6;
Elseif ($ c >=248) $ bits = 5;
Elseif ($ c >= 240) $ bits = 4;
Elseif ($ c> = 224) $ bits = 3;
Elseif ($ c> = 192) $ bits = 2;
Else return false;
If ($ I + $ bits)> $ len) return false;
While ($ bits> 1 ){
$ I ++;
$ B = ord ($ str [$ I]);
If ($ B <128
------ Solution --------------------
$ B> 191) return false;
$ Bits --;
}
}
}
Return true;
}
4. from php5.3, the mb_string extension provides the mb_check_encoding function.
If (mb_check_encoding ($ s, 'utf-8') echo 'yes ';