$zhStr = ‘您好,中国!’;
// gbk编码下每个中文字符所占字节为2
echo strlen($zhStr); // 输出:12
//utf-8编码下每个中文字符所占字节为3
echo strlen($zhStr); // 输出:18
所以,在做字符串截取或者长度判断的时候,要注意:长度是应该为 2的整数倍,还是3的整数倍。
不然就会报错:
Malformed UTF-8 characters, possibly incorrectly encoded
格式不正确的UTF-8字符,可能编码不正确