Php string encoding conversion functions (GBK/UTF-8)-PHP source code

Source: Internet
Author: User
Although php comes with an encoding conversion function, character loss occurs during use, in order to solve this problem, I have compiled two functions for converting the encoding of non-GBK character set into GBK and converting the encoding of non-UTF-8 character set into the function of UTF-8, I hope this will help you understand that php has built-in encoding and conversion functions, but you will encounter character loss problems during use, in order to solve this problem, I made up the compilation of the non-GBK character set encoding into GBK and the non-UTF-8 character set encoding into the UTF-8 function, I hope to help you

Script ec (2); script

Example.

The Code is as follows:

/**
* Convert non-GBK character set encoding to GBK
*
* @ Param mixed $ mixed source data
*
* @ Return mixed GBK format data
*/
Function charsetToGBK ($ mixed)
{
If (is_array ($ mixed )){
Foreach ($ mixed as $ k => $ v ){
If (is_array ($ v )){
$ Mixed [$ k] = charsetToGBK ($ v );
} Else {
$ Encode = mb_detect_encoding () ($ v, array ('ascii ', 'utf-8', 'gb2312', 'gbk', 'big5 '));
If ($ encode = 'utf-8 '){
$ Mixed [$ k] = iconv ('utf-8', 'gbk', $ v );
}
}
}
} Else {
$ Encode = mb_detect_encoding ($ mixed, array ('ascii ', 'utf-8', 'gb2312', 'gbk', 'big5 '));
// Var_dump ($ encode );
If ($ encode = 'utf-8 '){
$ Mixed = iconv ('utf-8', 'gbk', $ mixed );
}
}
Return $ mixed;
}


/**
* Convert non-UTF-8 character set encoding to UTF-8
*
* @ Param mixed $ mixed source data
*
* @ Return mixed UTF-8 format data
*/
Function charsetToUTF8 ($ mixed)
{
If (is_array ($ mixed )){
Foreach ($ mixed as $ k => $ v ){
If (is_array ($ v )){
$ Mixed [$ k] = charsetToUTF8 ($ v );
} Else {
$ Encode = mb_detect_encoding ($ v, array ('ascii ', 'utf-8', 'gb2312', 'gbk', 'big5 '));
If ($ encode = 'euc-cn '){
$ Mixed [$ k] = iconv ('gbk', 'utf-8', $ v );
}
}
}
} Else {
$ Encode = mb_detect_encoding ($ mixed, array ('ascii ', 'utf-8', 'gb2312', 'gbk', 'big5 '));
If ($ encode = 'euc-cn '){
$ Mixed = iconv ('gbk', 'utf-8', $ mixed );
}
}
Return $ mixed;
}

?>

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.