The difference between mb_convert_encoding and iconv in PHP _php tutorial

Source: Internet
Author: User
Tags translit
Mb_convert_encoding This function is used to convert the encoding. It turns out that the concept of program coding is not understood, but it seems to be a bit enlightened now. However, there is generally no coding problem in English, only the Chinese data will have this problem.

For example, when you write a program with Zend Studio or editplus, using GBK encoding, if the data needs to enter the database, and the database encoding is UTF8, then the data will be encoded conversion, or into the database will become garbled.

Mb_convert_encoding's usage is in the official view:

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 parameter

Str
The string being encoded.

To_encoding
The type of encoding that STR was being converted to.

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

See supported encodings.


Report a bug return value
The encoded string.

Report a bug Example

Example #1 mb_convert_encoding () Example

copy code

"!--? php
/* Convert Internal CHARACTE R Encoding to SJIS */
$str = mb_convert_encoding ($str, "SJIS");

/* Convert 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 ($st R, "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, $encoding 1, $encoding 2)

$str, to convert the encoded string
$encoding 1, the target code, such as UTF-8,GBK, is case-sensitive
$encoding 2, the original code, such as UTF-8,GBK, case can be

Example 1

The code is as follows Copy Code
$str = ' Script house: http://www.bKjia.c0m ';
Echo mb_convert_encoding ($str, "UTF-8"); Convert Encoding to Utf-8
?>

copy code
$str = ' scripting House:/HTTP/ Www.bKjia.c0m ';
Echo mb_convert_encoding ($str, "UTF-8", "GBK"),//known as GBK, converted to Utf-8
?>
The code is as follows Copy Code
$str = ' Script house: http://www.bKjia.c0m ';
Echo mb_convert_encoding ($str, "UTF-8", "Auto"); Unknown original code, after auto auto-detection, the conversion code is UTF-8
?>

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 friend "," UTF-8 "," GBK ");
?>

One more 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 function requires installation but first enable the mbstring extension library.

Another function in PHP, Iconv, is also used to convert string encodings, similar to functions on the upper function.

Here are a few more 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])
Need to enable Mbstring expansion Library, in php.ini; Extension=php_mbstring.dll in front of; Remove
Mb_convert_encoding can specify a variety of input encoding, it will automatically identify according to the content, but the execution efficiency is much worse than the iconv;


String Iconv (String in_charset, String out_charset, String str)
Note: The second parameter, in addition to specifying the encoding to be converted to, can also add two suffixes://translit and//ignore, where//translit automatically converts characters that cannot be converted directly into one or more approximate characters,//ignore Characters that cannot be converted are ignored, and the default effect is truncated from the first illegal character.
Returns the converted string or FALSE on failure.


Use:

The iconv is found to have an error converting the character "-" 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.

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 ");

In general, with Iconv, only use mb_convert_encoding function if you encounter an inability to determine what encoding the original encoding is, or if it cannot be displayed properly after iconv conversion.

http://www.bkjia.com/PHPjc/632197.html www.bkjia.com true http://www.bkjia.com/PHPjc/632197.html techarticle mb_convert_encoding This function is used to convert the encoding. It turns out that the concept of program coding is not understood, but it seems to be a bit enlightened now. But the English generally does not exist the code asks ...

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