Code for converting UTF-8 and GB2312 encoding using php

Source: Internet
Author: User
Php implements a function for mutual conversion between UTF-8 and GB2312 encoding. if you need it, refer The code is as follows:
/*************************************** *****
*
* Function name: get_utf8_to_gb ($ value)
* Use: utf8 encoded string to gb2312 encoding
* Author: Liu Xianzhong
* Date: 2011-11-09
*
**************************************** ****/
Function get_utf8_to_gb ($ value ){
$ Value_1 = $ value;
$ Value_2 = @ iconv ("UTF-8", "gb2312 // IGNORE", $ value_1); // use @ to resist errors, if a character does not have a corresponding character in the target character set, the subsequent part of the character is ignored. that is, the result string is incomplete and you need to use // IGNORE
$ Value_3 = @ iconv ("gb2312", "UTF-8 // IGNORE", $ value_2 );

If (strlen ($ value_1) = strlen ($ value_3 ))
{
Return $ value_2;
} Else
{
Return $ value_1;
}
}
/*************************************** *****
*
* Function name: get_gb_to_utf8 ($ value)
* Use: gb2312 encoded string to utf8 encoding
* Author: Liu Xianzhong
* Date: 2011-11-09
*
**************************************** ****/
Function get_gb_to_utf8 ($ value ){
$ Value_1 = $ value;
$ Value_2 = @ iconv ("gb2312", "UTF-8 // IGNORE", $ value_1 );
$ Value_3 = @ iconv ("UTF-8", "gb2312 // IGNORE", $ value_2 );
If (strlen ($ value_1) = strlen ($ value_3 ))
{
Return $ value_2;
} Else
{
Return $ value_1;
}
}
?>
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.