Differences between iconv and mb_convert_encoding transcoding functions

Source: Internet
Author: User
Iconv & mdash; Convertstringtorequestedcharacterencoding (PHP4 & gt; 4.0.5, PHP5) mb_convert_encoding & mdash; Convertcharacterencoding (PHP4 & gt; 4.0.6, PHP5) usage: 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. note that the second parameter can contain two suffixes in addition to specifying the encoding to be converted. // Transcoder automatically converts a character that cannot be directly converted into one or more similar characters. // IGNORE ignores the characters that cannot be converted, by default, the result is truncated from the first illegal character. for example:

// Convert "Welcome to WEB Development Network" from gb2312 encoding to UTF-8 encoding echo iconv ('gb2312', 'utf-8, "Welcome to WEB Development Network ");

Small traps for iconv transcoding in PHP

It is found that iconv will encounter an error when converting the character "-" (character after ascii code 150) to gb2312. without the ignore parameter, all strings after this character cannot be saved. In any case, the "-" cannot be converted successfully or output. In addition, mb_convert_encoding does not have this bug.

Generally, iconv is used. the mb_convert_encoding function is used only when the encoding of the original encoding cannot be determined or The iconv cannot be normally displayed after conversion.

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 will 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

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.