Introduced
In front-end development, in order to make Chinese in different environments can be very good display, the general is to convert Chinese into Unicode format, that is, \u4f60, such as: "Hello Ah," The Unicode Encoding "\u4f60\u597d\u554a".
JS to convert Chinese into Unicode encoding is very simple.
function Convert2unicode (str) {return str.replace (/[\u0080-\uffff]/g,function ($) {var tmp = $0.charcodeat (0). ToString (+); return "\u" + new Array (5-tmp.length). Join (' 0 ') + tmp;});
and also very simple, direct alert out or innerHTML to the DOM node can be.
However, if the \u4f60\u597d\u554a "character is passed to php,php, it cannot be directly echo or other action. Direct echo words or native characters cannot be converted to Chinese automatically.
?
PHP convert Unicode to Utf-8 method
Json_encode, Json_decode methods are available in php5.0 and above. When using the Json_encode variable, if the variable contains Chinese, the Chinese will be converted to Unicode format. So is it possible to convert Unicode to Chinese through json_decode? The actual test discovery is possible, but there are some problems with single string discovery.
For simple strings, it is found that sometimes the json_decode is used, and the result is directly empty. But replace the string with an array and then turn it on. The following code is encapsulated below.
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 very well.
Attach JS to entity characters and PHP to convert entity characters to Chinese characters
JS converts Chinese characters to entity characters:
function Convert2entity (str) {var len = Str.length;var re = [];for (var i = 0; I < len; i++) {var code = Str.charc Odeat (i); if (code >) {re.push (' &# ' + code + '; ');} else {Re.push (Str.charat (i));}} Return Re.join (");}
PHP converts an entity character to a utf-8 Kanji method:
function Entity2utf8onechar ($unicode _c) {$unicode _c_val = intval ($unicode _c); $f =0x80;//10000000$str = "";// U-00000000-u-0000007f:0xxxxxxxif ($unicode _c_val <= 0x7F) {$str = Chr ($unicode _c_val);}//u-00000080-u-00000 7ff:110xxxxx 10xxxxxx Else if ($unicode _c_val >= 0x80 && $unicode _c_val <= 0x7ff) {$h =0xc0; 11000000 $c 1 = $unicode _c_val >> 6 | $h; $c 2 = ($unicode _c_val & 0x3F) | $f; $str = Chr ($c 1). chr ($c 2); U-00000800-u-0000ffff:1110xxxx 10xxxxxx 10xxxxxxelse if ($unicode _c_val >= 0x800 && $unicode _ C_val <= 0xFFFF) {$h =0xe0;//11100000 $c 1 = $unicode _c_val >> | $h; $c 2 = (($unicode _c_val &am P 0XFC0) >> 6) | $f; $c 3 = ($unicode _c_val & 0x3F) | $f; $str =chr ($c 1). chr ($c 2). chr ($c 3); U-00010000-u-001fffff:11110xxx 10xxxxxx 10xxxxxx 10xxxxxxelse if ($unicode _c_val >= 0x10000 && ; $unicode _c_val <= 0x1fffff) {$h =0xf0; 11110000 $c 1 = $unicode _c_val >> 18 | $h; $c 2 = (($unicode _c_val & 0x3f000) >>12) | $f; $c 3 = (($unicode _c_val & 0xfc0) >>6) | $f; $c 4 = ($unicode _c_val & 0x3F) | $f; $str = Chr ($c 1). chr ($c 2). chr ($c 3). chr ($c 4); U-00200000-U-03FFFFFF:111110XX 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxxelse if ($unicode _c_val >= 0x200000 & ;& $unicode _c_val <= 0x3ffffff) {$h =0xf8;//11111000 $c 1 = $unicode _c_val >> | $h; $c 2 = (($unicode _c _val & 0xfc0000) >>18) | $f; $c 3 = (($unicode _c_val & 0x3f000) >>12) | $f; $c 4 = (($unicode _c_val & 0xfc0) >>6) | $f; $c 5 = ($unicode _c_val & 0x3F) | $f; $str = Chr ($c 1). chr ($c 2). chr ($c 3). chr ($c 4). chr ($c 5); u-04000000-u-7fffffff:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxxelse if ($unicode _c_val >= 0x400000 0 && $unicode _c_val <= 0x7FFFFFFF) {$h =0xfc;/11111100 $c 1 = $unicode _c_val >> 30 | $h; $c 2 = (($unicode _c_val & 0x3f000000) >>24) | $f; $c 3 = (($unicode _c_val & 0xfc0000) >>18) | $f; $c 4 = (($unicode _c_val & 0x3f000) >>12) | $f; $c 5 = (($unicode _c_val & 0xfc0) >>6) | $f; $c 6 = ($unicode _c_val & 0x3F) | $f; $str = Chr ($c 1). chr ($c 2). chr ($c 3). chr ($c 4). chr ($c 5). chr ($c 6); return $STR;} function Entities2utf8 ($unicode _c) {$unicode _c = preg_replace ("/\&\# ([\da-f]{5}) \;/es", "Entity2utf8onechar (' \\1 ') ", $unicode _c); return $unicode _c;}
Entity2utf8onechar method from HTTP://BLOG.SINA.COM.CN/S/blog_48d7f3f40100o6ak.html
How to use:
$utf 8chars = Entities2utf8 ("Ah, hello.");
?
Transferred from: http://www.welefen.com/php-unicode-to-utf8.html