php 5.2.4 iconv函数TRANSLIT 有时候不靠谱

WBOY
Release: 2016-06-13 12:49:45
Original
937 people have browsed it

php 5.2.4 iconv函数TRANSLIT 有时不靠谱

php 版本 5.2.4

问题:文件字符串转码时按字节截断不当

比如

$str = “公司”;                                  //默认以UTF-8编码
$str = Simple_Util_String::msubstr($str, 4);	// 按字节截取前4个字节,原字符串有6个字节
$str .= “adfadsfasdfadsfasdf”; 			//拼接后面的字符串  (执行下一步后,这些字符就消失了)
$str = iconv("UTF-8", "GBK//TRANSLIT",$str);    // 转码 , 遇到不认识的字符串进行转写
Copy after login

执行此步时,“公司”的“司”字编码不完整,iconv不认识,但iconv没有转写,而是做了截断。导致$str后面的字符串也没有转换成功,“adfadsfasdfadsfasdf”丢失了。在一些商业产品,尤其是有关统计的系统里,这是很危险的。

解决方法是:

改变iconv第二个参数。

$str = iconv("UTF-8","GBK//IGNORE", $str);
Copy after login

总结:iconv的TRANSLIT并不靠谱,遇到不认识也不能转写的字符串,也可能截断。保险的方法是用IGNORE。

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template