In PHP, if we convert a uft8 string into gbk or gb2312, there will be problems with garbled characters or loss. Because of the problem of gbk encoding range and uft8 encoding range, below we simply list a gbk and utf8 encoding range table. You will know the reason after reading it.
1. Coding range
1. GBK (GB2312/GB18030)
x00-xff GBK double-byte encoding range
x20-x7f ASCII
xa1-xff Chinese
x80-xff Chinese
2. UTF-8 (Unicode)
u4e00-u9fa5 (Chinese)
x3130-x318F (Korean
xAC00-xD7A3 (Korean)
u0800-u4e00 (Japanese)
ps: Korean is a character larger than [u9fa5]
Example
The code is as follows
代码如下 |
复制代码 |
$c = '测试•字符传换•五一快乐!';
echo iconv('utf-8', 'gbk',$c);
|
|
Copy code
|
$c = 'Test•Character transfer•Happy May Day! ';
echo iconv('utf-8', 'gbk',$c);
代码如下 |
复制代码 |
$c = '测试•字符传换•五一快乐!';
echo iconv('utf-8', 'gbk//IGNORE',$c);
|
Only output: test and then all will be lost."
Solution:
代码如下 |
复制代码 |
echo $str= ‘你好,这里是卖咖啡!';
echo ' ';
echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8
echo ' ';
echo iconv_substr($str, 1, 1, 'UTF-8'); //按字符个数截取而非字节
print_r(iconv_get_encoding()); //得到当前页面编码信息
echo iconv_strlen($str, 'UTF-8'); //得到设定编码的字符串长度
?> |
Add //IGNORE |
The code is as follows |
Copy code |
$c = 'Test•Character transfer•Happy May Day! ';
echo iconv('utf-8', 'gbk//IGNORE',$c);
Input and output: Test character transfer Happy May Day!
Example 2
The code is as follows
|
Copy code
|
echo ' ';
echo iconv('GB2312', 'UTF-8', $str); //Convert the string encoding from GB2312 to UTF-8
echo ' ';
echo iconv_substr($str, 1, 1, 'UTF-8'); //Truncate by the number of characters instead of bytes
print_r(iconv_get_encoding()); //Get the current page encoding information
echo iconv_strlen($str, 'UTF-8'); //Get the string length of the set encoding
?>
http://www.bkjia.com/PHPjc/632129.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632129.htmlTechArticleIn php, if we convert the uft8 string into gbk or gb2312, there will be garbled or missing problems, because gbk Regarding the issue of encoding range and uft8 encoding range, we briefly list one below...
|
|