How does php convert unicode to UTF-8 encoding?

Source: Internet
Author: User
Encoding conversion has always been a headache. we usually encounter page garbled characters due to some encoding problems. The following section provides an example of converting unicode encoding to UTF-8 encoding. let's take a look. in front-end development, in order to make Chinese in different environments very good... encoding conversion has always been a headache. we usually encounter page garbled characters due to some encoding problems. The following section provides an example of converting unicode encoding to UTF-8 encoding. let's take a look.

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

It is very easy to convert Chinese to unicode encoding in JS. 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 simple. you can use alert directly or use 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 using the json_encode variable, 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 a simple string, it is found that sometimes the result is converted using json_decode and the result is empty, but the string is replaced with an array and then converted. The following is the encapsulated 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, and convert JavaScript encoding to entity characters and convert php encoding to Chinese characters.

Js converts Chinese characters into entity characters:

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; }

Php converts an object character to a UTF-8 character:

function entity2utf8onechar($unicode_c){ $unicode_c_val = intval($unicode_c); $f=0x80; // 10000000 $str = ""; // U-00000000 - U-0000007F:   0xxxxxxx if($unicode_c_val <= 0x7F){         $str = chr($unicode_c_val);     }     //U-00000080 - U-000007FF:  110xxxxx 10xxxxxx     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:  1110xxxx 10xxxxxx 10xxxxxx else if($unicode_c_val >= 0x800 && $unicode_c_val <= 0xFFFF){         $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 10xxxxxx 10xxxxxx 10xxxxxx 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 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 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 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 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 (" ");


Article address:

Reprint at will ^ please include the address of this 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.