Beginner transcoding problems

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.