Differences between mb_convert_encoding and iconv in php

Source: Internet
Author: User

Mb_convert_encoding is used to convert the encoding. I have never understood the concept of program encoding, but now it seems a bit open. However, the English language generally does not have Encoding Problems. Only Chinese data can have this problem.

For example, when you use Zend Studio or Editplus to write a program, you use gbk encoding. If the data needs to be imported into the database and the database is encoded as utf8, You need to encode and convert the data, otherwise, the database will become garbled.

For the usage of mb_convert_encoding, refer to the official website:

Mb_convert_encoding-Convert character encoding

Report a bug description
String mb_convert_encoding (string $ str, string $ to_encoding [, mixed $ from_encoding])
Converts the character encoding of string str to to_encoding from optionally from_encoding.

Report a bug Parameters

Str
The string being encoded.

To_encoding
The type of encoding that str is being converted.

From_encoding
Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used.

See supported encodings.


Report a bug Return Value
The encoded string.

Example of Report a bug

Example #1 mb_convert_encoding () example

The Code is as follows: Copy code

<? Php
/* Convert internal character encoding to SJIS */
$ Str = mb_convert_encoding ($ str, "SJIS ");

/* Converter EUC-JP to UTF-7 */
$ Str = mb_convert_encoding ($ str, "UTF-7", "EUC-JP ");

/* 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 ");
?>

Mb_convert_encoding ($ str, $ encoding1, $ encoding2)

$ Str: string to be converted to Encoding
$ Encoding1: Target encoding, such as UTF-8, gbk, and uppercase/lowercase
$ Encoding2: original encoding, such as UTF-8, gbk, and uppercase/lowercase

Instance 1

The Code is as follows: Copy code
<? Php
$ Str = 'feet home: http://www.bKjia. c0m ';
Echo mb_convert_encoding ($ str, "UTF-8"); // encode to UTF-8
?>

 

The Code is as follows: Copy code
<? Php
$ Str = 'feet home: http://www.bKjia. c0m ';
Echo mb_convert_encoding ($ str, "UTF-8", "GBK"); // It is known that the original encoding is GBK and is converted to UTF-8
?>
The Code is as follows: Copy code
<? Php
$ Str = 'feet home: http://www.bKjia. c0m ';
Echo mb_convert_encoding ($ str, "UTF-8", "auto"); // unknown original encoding, converted to UTF-8 after auto Detection
?>

Make a GBK To UTF-8

 

The Code is as follows: Copy code
<? Php
Header ("content-Type: text/html; charset = Utf-8 ");
Echo mb_convert_encoding ("??? S my friends "," UTF-8 "," GBK ");
?>

Another GB2312 To Big5

 

The Code is as follows: Copy code
<? Php
Header ("content-Type: text/html; charset = big5 ");
Echo mb_convert_encoding ("you are my friend", "big5", "GB2312 ");
?>

However, to use the above functions, you need to install but enable mbstring extension Library first.

Another function iconv in PHP is also used to convert string encoding, similar to the function above.

The following are examples:

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:
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding])
Enable mbstring extension Library first, and remove the extension library before extension = php_mbstring.dll in php. ini.
Mb_convert_encoding can specify multiple input encodings, which are automatically identified based on the content, but the execution efficiency is much lower than that of iconv;


String iconv (string in_charset, string out_charset, string str)
Note: In addition to specifying the encoding to be converted, you can also add two suffixes: // Transcoder and // IGNORE, // 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 invalid character.
Returns the converted string or FALSE on failure.


Usage:

It is found that iconv will encounter an error when converting characters "-" To gb2312. If the ignore parameter is not available, 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.

The Code is as follows: Copy code
/* 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 ");

Example:

The Code is as follows: Copy code
$ Content = iconv ("GBK", "UTF-8 // IGNORE", $ content );
$ Content = mb_convert_encoding ($ content, "UTF-8", "GBK ");

 

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 conversion fails to be displayed normally.

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.