Phpiconv function usage example and precautions

Source: Internet
Author: User
Phpiconv function usage example and precautions

  1. Echo $ str = 'Hi, it's coffee sale! ';
  2. Echo'
    ';
  3. Echo iconv ('gb2312', 'utf-8', $ str); // Encode the string from GB2312 to UTF-8
  4. Echo'
    ';
  5. Echo iconv_substr ($ str, 1, 1, 'utf-8'); // truncate by number of characters rather than bytes
  6. Print_r (iconv_get_encoding (); // Obtain the encoding information of the current page.
  7. Echo iconv_strlen ($ str, 'utf-8'); // you can specify the length of the encoded string.
  8. // There is also a bbs.it-home.org for this use
  9. $ Content = iconv ("UTF-8", "gbk // transcoder", $ content );
  10. ?>

Notes for using iconv functions in php

When iconv () is used to convert a character that is not supported by the output character encoding, such as iconv ('utf-8', 'gb2312 ', 'hangzhou'), the following error occurs: notice: iconv () [function. iconv]: Detected an illegal character in input string... because GB2312 represents simplified Chinese, it does not support more complex Chinese characters such as "simplified" and some special characters. of course, an error is returned.

There are two solutions: 1. expand the output character encoding range, such as iconv ('utf-8', 'gbk', 'authorization, because GBK supports a wider range of characters, 2. add "// IGNORE" after the output character encoding string, such as iconv ('utf-8', 'gb2312 // IGNORE ', 'unconvertible characters are ignored to avoid errors but cannot be correctly output (that is, no blank space or output ).

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.