Beginner transcoding problems
Required url1_'test.txt '; $ read = fopen ($ url, 'r') or die ('open failed'); $ text = fread ($ read, 100 ); fclose ($ read); echo iconv ('utf-8', 'gbk', 'this is a test ').'
'; # This sentence OKecho iconv ('utf-8', 'gbk', $ text ).'
'; # This error message
The first echo is normal, and the second echo always reports the following error:
Notice: iconv (): Detected an illegal character in input string
Test.txt is in UTF-8 format.
Reply to discussion (solution)
Your text contains characters out of the gbk range.
Echo iconv ('utf-8', 'gbk // IGNORE ', $ text)
// IGNORE can IGNORE the words it does not know and then go down to report no error, // transcoder is to cut off the words it does not know and the content behind it, and report an error
The three-byte part of UTF-8 is equal to that of gbk.
However, the two-byte part of UTF-8 does not have a corresponding part in gbk (not all)
First: iconv ("UTF-8", "GB2312 // IGNORE", $ data );
Type 2: $ outstr = mb_convert_encoding ($ instr, 'utf-8', 'gbk ',); if iconv is used to report an error, mb_convert_encoding function is used. this function character set is more extensive than iconv and generally does not report iconv errors.
Echo iconv ('utf-8', 'gbk // IGNORE ', $ text)
Thank you for your success.