Beginner transcoding Issues

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

  • 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.