How does php convert unicode to UTF-8 encoding?

Source: Internet
Author: User
Tags chr php code

In front-end development, in order to make Chinese display better in different environments, it is generally to convert Chinese to unicode format, that is, u4f60, for example: "Hello," unicode encoding is "u4f60u597du554a ".

Converting Chinese to unicode encoding in JS is very easy.

JS code:
Function convert2Unicode (str ){
Return str. replace (/[u0080-uffff]/g,
Function ($0 ){
Var tmp = $0. charCodeAt (0). toString (16 );
Return "u" + new Array (5-tmp. length). join ('0') + tmp;
});
}

Reversing is also very easy. It can be used either directly out of alert or innerHTML to the dom node.
However, if you pass the u4f60u597du554a character to php, php cannot directly echo or perform other operations. Directly echo is a native character and cannot be automatically converted to Chinese.
Php converts unicode to UTF-8
The json_encode and json_decode methods are provided in php5.0 and later versions. When the json_encode variable is used, if the variable contains Chinese characters, the Chinese characters are converted to unicode format. So is it possible to convert unicode to Chinese through json_decode? The actual test results are acceptable, but some problems are found for a single string.
For simple strings, it is found that the result is directly empty when json_decode is used for conversion. But replace the string with an array and then convert it. The following is the encapsulated code:
Php code:
Function unicode2utf8 ($ str ){
If (! $ Str) return $ str;
$ Decode = json_decode ($ str );
If ($ decode) return $ decode;
$ Str = '["'. $ str. '"]';
$ Decode = json_decode ($ str );
If (count ($ decode) = 1 ){
Return $ decode [0];
        }
Return $ str;
}

This method can be used to convert unicode encoding to UTF-8 encoding.
How to convert JavaScript to entity characters and convert php to Chinese characters
Js converts Chinese characters into entity characters:
Js code:
Function convert2Entity (str ){
Var len = str. length;
Var re = [];
For (var I = 0; I <len; I ++ ){
Var code = str. charCodeAt (I); if (code> 256 ){
Re. push ('& #' + code + ';');
} Else {
Re. push (str. charAt (I ));
        }
    }
Return re. join ('');
}
Php converts an object character to a UTF-8 character:
Php code:
Function entity2utf8onechar ($ unicode_c ){
$ Unicode_c_val = intval ($ unicode_c );
$ F = 0x80; // 10000000
$ Str = "";
// U-00000000-U-0000007F: 0 xxxxxxx
If ($ unicode_c_val <= 0x7F) {$ str = chr ($ unicode_c_val);} // U-00000080-U-000007FF: 110 xxxxx 10 xxxxxx else if ($ unicode_c_val> = 0x80 & $ unicode_c_val <= 0x7FF) {$ h = 0xC0; // 11000000 $ c1 = $ unicode_c_val> 6 | $ h;
$ C2 = ($ unicode_c_val & 0x3F) | $ f;
$ Str = chr ($ c1). chr ($ c2 );
    }
// U-00000800-U-0000FFFF: 1110 xxxx 10 xxxxxx 10 xxxxxx
Else if ($ unicode_c_val> = 0x800 & $ unicode_c_val <= 0 xFFFF) {$ h = 0xE0; // 11100000 $ c1 = $ unicode_c_val> 12 | $ h;
$ C2 = ($ unicode_c_val & 0xFC0)> 6) | $ f;
$ C3 = ($ unicode_c_val & 0x3F) | $ f;
$ Str = chr ($ c1). chr ($ c2). chr ($ c3 );
    }
/U-00010000-U-001FFFFF: 11110xxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
Else if ($ unicode_c_val> = 0x10000 & $ unicode_c_val <= 0x1FFFFF) {$ h = 0xF0; // 11110000 $ c1 = $ unicode_c_val> 18 | $ h;
$ C2 = ($ unicode_c_val & 0x3F000)> 12) | $ f;
$ C3 = ($ unicode_c_val & 0xFC0)> 6) | $ f;
$ C4 = ($ unicode_c_val & 0x3F) | $ f;
$ Str = chr ($ c1). chr ($ c2). chr ($ c3). chr ($ c4 );
    }
// U-00200000-U-03FFFFFF: 111110xx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
Else if ($ unicode_c_val> = 0x200000 & $ unicode_c_val <= 0x3FFFFFF) {$ h = 0xF8; // 11111000 $ c1 = $ unicode_c_val> 24 | $ h;
$ C2 = ($ unicode_c_val & 0xFC0000)> 18) | $ f;
$ C3 = ($ unicode_c_val & 0x3F000)> 12) | $ f;
$ C4 = ($ unicode_c_val & 0xFC0)> 6) | $ f;
$ C5 = ($ unicode_c_val & 0x3F) | $ f;
$ Str = chr ($ c1). chr ($ c2). chr ($ c3). chr ($ c4). chr ($ c5 );
    }
// U-04000000-U-7FFFFFFF: 1111110x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
Else if ($ unicode_c_val> = 0x4000000 & $ unicode_c_val <= 0x7FFFFFFF) {$ h = 0xFC; // 11111100 $ c1 = $ unicode_c_val> 30 | $ h;
$ C2 = ($ unicode_c_val & 0x3F000000)> 24) | $ f;
$ C3 = ($ unicode_c_val & 0xFC0000)> 18) | $ f;
$ C4 = ($ unicode_c_val & 0x3F000)> 12) | $ f;
$ C5 = ($ unicode_c_val & 0xFC0)> 6) | $ f;
$ C6 = ($ unicode_c_val & 0x3F) | $ f;
$ Str = chr ($ c1). chr ($ c2). chr ($ c3). chr ($ c4). chr ($ c5). chr ($ c6 );
    }
Return $ str;
}
Function entities2utf8 ($ unicode_c ){
$ Unicode_c = preg_replace ("/& # ([da-f] {5});/es", "entity2utf8onechar ('\ 1')", $ unicode_c );
Return $ unicode_c;
}

Usage:

$ Utf8chars = entities2utf8 (" ");

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.