PHP character encoding Conversion

Source: Internet
Author: User
Tags translit
This article mainly introduces the PHP character encoding conversion method, the steps of the method is more detailed, hope can help everyone.

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 extension library, in php.ini; Extension=php_mbstring.dll in front of; Remove


String Iconv (String in_charset, String out_charset, String str)
Attention:
The second parameter, in addition to specifying the encoding to be converted to, can also add two suffixes://translit and//ignore,
which
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.
Returns the converted string or FALSE on failure.

Use:
1. 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. Anyway, this

"-" can not be converted successfully, cannot output. Another mb_convert_encoding does not have this bug.
2. Mb_convert_encoding can specify a variety of input codes, which will be automatically recognized according to the content, but the execution efficiency is much worse than the iconv, such as: $str =

Mb_convert_encoding ($str, "EUC-JP", "Ascii,jis,euc-jp,sjis,utf-8"); " Ascii,jis,euc-jp,sjis,utf-8 "in the order of different effects also have

Difference
3. In general, with Iconv, only when encountering the inability to determine the encoding of the original encoding, or iconv conversion can not be normal display when the mb_convert_encoding function

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

$str = mb_convert_encoding ($str, "Ucs-2le", "JIS, Eucjp-win, Sjis-win");

$str = mb_convert_encoding ($str, "EUC-JP", "Auto");

Example:
$content = Iconv ("GBK", "UTF-8", $content);
$content = mb_convert_encoding ($content, "UTF-8", "GBK");


Conclusion

1, mb_convert_encoding () The function will automatically identify the code according to the content, but the execution efficiency is worse than iconv;
2, then there is a saying that iconv () in some character conversion will not be normal, the internet has a saying is: Found Iconv in the conversion character "-" to gb2312 error, if there is no ignore parameter, all the string after the 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.
3, mb_convert_encoding () is PHP extension function, to open the extension of the library to use, and Iconv is a PHP built-in function, do not need to open the extension library can be used.
4, in general, with Iconv, only when encountered can not determine the original encoding is what encoding, or iconv conversion can not be normal display when the mb_convert_encoding function

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.