Determine whether there is any Chinese language in the content, and then return Chinese characters
Source: Internet
Author: User
Determine whether there is any Chinese character in the content, and then return the Chinese character. first, I use preg_match_all ('/[\ x80-\ xff]. /', & nbsp; $ s, $ arr); check whether there are any Chinese characters, then print out $ arr, check which Chinese characters are available, and modify accordingly, but printing $ arr is garbled, with several GB2312 to UTF-8 method is not good. You can help 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 ';
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.