php gbk转换utf8丢失字符与乱码解决办法
在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'); //得到设定编码的字符串长度 ?> |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools
