$url = ' Test.txt '; $read = fopen ($url, ' r ') or Die (' Open failed '); $text =fread ($read); fclose ($read); Echo iconv (' UTF-8 ', ' GBK ', ' This is a test '). '
'; #这句OKecho iconv (' UTF-8 ', ' GBK ', $text). '
'; #这句报错
The first echo above is normal, and the second echo always reports the following error:
Notice:iconv (): detected an illegal character in input string
The Test.txt file is in UTF-8 format.
Reply to discussion (solution)
You have characters in your text that are outside the GBK range
echo iconv (' UTF-8 ', ' Gbk//ignore ', $text)
Ignore can ignore the words it does not know and then go down without error,//translit is to cut off the words it does not know and the content behind, error
The three-byte portion of the Utf-8 is equivalent to the GBK
But the two-byte portion of Utf-8 has no counterpart (not all) in GBK
The first type: Iconv ("Utf-8″," Gb2312//ignore ", $data);
The second type: $outstr = mb_convert_encoding ($instr, ' UTF-8 ', ' GBK ',); With iconv error, use mb_convert_encoding function This function character set than iconv generally not reported iconv error
echo iconv (' UTF-8 ', ' Gbk//ignore ', $text) Try It
Thank you upstairs, the successful solution.