Home  >  Article  >  Backend Development  >  新手转码问题

新手转码问题

WBOY
WBOYOriginal
2016-06-23 14:39:34941browse

	$url='test.txt';	$read = fopen($url,'r') or die('打开失败');	$text=fread($read,100);	fclose($read);	echo iconv('UTF-8','GBK','这是一个测试').'<br/>';#这句OK	echo iconv('UTF-8','GBK',$text).'<br/>';#这句报错


以上第一个echo正常,第二个echo就老是报以下错误:
 Notice: iconv(): Detected an illegal character in input string

test.txt文件为UTF-8格式。


回复讨论(解决方案)

你的文本内有超出gbk范围的字符

echo iconv('UTF-8','GBK//IGNORE',$text)
//IGNORE能忽略掉它不认识的字接着往下转不报错,//TRANSLIT是截掉它不认识的字及其后面的内容,报错

utf-8 的 三字节部分与 gbk 对等
但 utf-8 的二字节部分在 gbk 中无对应部分(不是全部)

第一种:iconv(“UTF-8″,”GB2312//IGNORE”,$data)  ;
第二种:$outstr = mb_convert_encoding($instr,'UTF-8','GBK',); 用iconv报错的话就用mb_convert_encoding函数这个函数字符集比iconv的广泛一般不会报iconv的错

echo iconv('UTF-8','GBK//IGNORE',$text)试试

感谢楼上各位,成功解决。

Statement:
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