Home > php教程 > php手册 > php gbk转换utf8丢失字符与乱码解决办法

php gbk转换utf8丢失字符与乱码解决办法

WBOY
Release: 2016-06-13 09:57:06
Original
943 people have browsed it

在php中如果我们把uft8字符串转换成gbk或gb2312都会出现乱码或丢失的问题,因为gbk编码范围与uft8编码范围的问题,下面我们简单的列了一个gbk与utf8编码范围表,看了就知道原因了。

一、编码范围
1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文
x80-xff 中文


2. UTF-8 (Unicode)
u4e00-u9fa5 (中文)
x3130-x318F (韩文
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)

ps: 韩文是大于[u9fa5]的字符

 代码如下 复制代码

$c = '测试•字符传换•五一快乐!';

echo iconv('utf-8', 'gbk',$c);

只会输出: 测试 后出全会丢失"."

解决方法:

加 //IGNORE

 代码如下 复制代码

$c = '测试•字符传换•五一快乐!';

echo iconv('utf-8', 'gbk//IGNORE',$c);

输入出:测试字符传换五一快乐!

例2

 

 代码如下 复制代码
  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'); //得到设定编码的字符串长度
?>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template