The difference between iconv and mb_convert_encoding transcoding functions

Source: Internet
Author: User
Keywords The difference between iconv and mb_convert_encoding transcoding functions

Iconv-convert string to requested character encoding (PHP 4 >= 4.0.5, PHP 5)

Mb_convert_encoding-convert character encoding (PHP 4 >= 4.0.6, PHP 5)

Usage:

Iconv

String Iconv (String $in _charset, String $out _charset, String $str)

Converts a string from one specified encoding to another specified encoding. Note that the second parameter adds two suffixes, in addition to the encoding you can specify to convert to. Translit automatically converts characters that cannot be converted directly into one or more approximate characters,//ignore ignores characters that cannot be converted, and the default effect is to truncate from the first illegal character, for example:

Small traps using iconv transcoding in PHP

It is found that Iconv has an error converting the character "-" (characters after ASCII 150) to gb2312, and if there is no ignore parameter, all strings after that character cannot be saved. In any case, this "-" can not be converted successfully, unable to output. Another mb_convert_encoding does not have this bug.

In general, with Iconv, only use the Mb_convert_encoding function if you encounter an inability to determine what encoding the original encoding is, or if the Iconv conversion fails to display properly.

From_encoding is specified by character code name before conversion. It can be array or STRING-COMMA separated enumerated list. If It is not specified, the internal encoding would be used.

/* Auto detect encoding from JIS, Eucjp-win, Sjis-win, then convert str to Ucs-2le */
$str = mb_convert_encoding ($str, "Ucs-2le", "JIS, Eucjp-win, Sjis-win");
/* "Auto" is expanded to "ascii,jis,utf-8,euc-jp,sjis" */
$str = mb_convert_encoding ($str, "EUC-JP", "Auto");

12 Next Page
  • Related Article

    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.