PHP character encoding conversion function Iconv mb_convert_encoding comparison

Source: Internet
Author: User

When using PHP to handle strings, we often encounter the problem of character encoding conversion, have you ever encountered a ICONV conversion failure?

Found the problem, the search on the Internet, only to find Iconv original bug, encountered some rare words will not be able to convert, of course, the configuration of the second parameter, you can slightly compensate for the default defects, not to be able to convert is truncated, use the following

Iconv ("Utf-8″," GB2312//IGNORE ", $data)  ;

In this way, when the conversion fails, it will ignore the failure, continue to convert the following content, this is a solution to the problem, but in order to ensure the success of the conversion, we can use another conversion function (mb_convert_encoding), the data online said that the function is not very high efficiency, In addition, this function can also omit the third parameter, automatically identify the content encoding, but it is best not to use, affect efficiency, also need to pay attention to, mb_convert_encoding and iconv parameter order is not the same, must pay attention to.

A simple usage of two functions:

Iconv

String iconv (string $in _charset , string $out _charset , string $str )

First parameter: Encoding of content origin

Second parameter: Target encoding

Third argument: the string to go

function return string

<? PHP $instr = ' Test '; // GBK Turn UTF-8 $outstr Iconv (' GBK ', 'utf-8′, $instr);? >
Mb_convert_encodingstring mb_convert_encoding (String $str, String $to _encoding [, Mixed $from _encoding]) First argument: the string to be processed Second parameter: Target encoding third parameter: Content original encoding
<? PHP $instr = ' Test '; // GBK Turn UTF-8 $outstr = mb_convert_encoding ($instr, ' UTF-8 ', ' GBK ',); >

transcoding problem when using mb_convert_encoding comparison insurance.

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.