In php, If we convert a uft8 string to gbk or gb2312, garbled characters may be lost because of the gbk encoding range and uft8 encoding range problems, the following is a simple list of gbk and utf8 encoding range tables. You can see the reason.
I. Encoding range
1. GBK (GB2312/GB18030)
X00-xff GBK dubyte encoding range
X20-x7f (ASCII)
Xa1-xff (Chinese)
X80-xff (Chinese)
2. UTF-8 (Unicode)
U4e00-u9fa5)
X3130-x318F (Korean
XAC00-xD7A3 (Korean)
U0800-u4e00 (Japanese)
Ps: Korean is a character greater than [u9fa5]
Example
The Code is as follows: |
Copy code |
$ C = 'test • character transfer • happy May Day! '; Echo iconv ('utf-8', 'gbk', $ c ); |
Only output: After the test, the plenary session is lost "."
Solution:
Add // IGNORE
The Code is as follows: |
Copy code |
$ C = 'test • character transfer • happy May Day! '; Echo iconv ('utf-8', 'gbk // IGNORE ', $ c ); |
Input: Test character transfer for happy May Day!
Example 2
The Code is as follows: |
Copy code |
<? Php Echo $ str = 'Hi, it's coffee sale! '; Echo '<br/> '; Echo iconv ('gb2312', 'utf-8', $ str); // encode the string from GB2312 to UTF-8 Echo '<br/> '; Echo iconv_substr ($ str, 1, 1, 'utf-8'); // truncate by number of characters rather than bytes Print_r (iconv_get_encoding (); // obtain the encoding information of the current page. Echo iconv_strlen ($ str, 'utf-8'); // you can specify the length of the encoded string. ?> |