unicode编码转换教程(php将unicode编码转为utf-8方法)

介绍在前端开发中,为了让中文在不同的环境下都能很好的显示,一般是将中文转化为unicode格式,即\\u4f60,比如:\"你好啊\"的unicode编码为\"\\u4f60\\u597d\\u554a\"。JS里将

介绍

在前端开发中,为了让中文在不同的环境下都能很好的显示,一般是将中文转化为unicode格式,即\\u4f60,比如:\"你好啊\"的unicode编码为\"\\u4f60\\u597d\\u554a\"。

JS里将中文转为unicode编码很简单。

<span><span>function</span> <span>convert2Unicode</span>(<span>str</span>) </span>{<span>return</span> str.replace(<span>/[\\u0080-\\uffff]/g</span>,<span><span>function</span>(<span>$<span>0</span></span>) </span>{<span>var</span> tmp = $<span>0.</span>charCodeAt(<span>0</span>).toString(<span>16</span>);<span>return</span> <span>\"\\u\"</span> + <span>new</span> <span>Array</span>(<span>5</span> - tmp.length).join(<span>\'0\'</span>) + tmp;});}

unicode编码转换教程(php将unicode编码转为utf-8方法)

反转也很简单,直接alert出来或者innerHTML到dom节点里都可以。

但如果将\\u4f60\\u597d\\u554a\"字符传递给php,php就不能直接echo或者其他操作了。直接echo的话还是原生的字符,不能自动转化为中文。

php将unicode转为utf-8方法

在php5.0及以上版本中提供了json_encode, json_decode方法。在使用json_encode变量的时候,如果变量里含有中文的话,会将中文转为unicode格式。所以在想是否可以通过json_decode将unicode转为中文呢?实际测试发现是可以的,但对单一的字符串发现有些问题。

对于简单的字符串,发现有时候使用json_decode转的化,结果直接为空了。但将字符串替换为数组然后在转就可以了。下面为封装的代码:

<span><span>function</span> <span>unicode2utf8</span><span>($str)</span></span>{<span>if</span>(!$str) <span>return</span> $str;$decode = json_decode($str);<span>if</span>($decode) <span>return</span> $decode;$str = <span>\'[\"\'</span> . $str . <span>\'\"]\'</span>;$decode = json_decode($str);<span>if</span>(count($decode) == <span>1</span>){<span>return</span> $decode[<span>0</span>];}<span>return</span> $str;}

使用这个方法可以很好的将unicode编码转为utf-8编码。

附上js转为实体字符和php将实体字符转为汉字的方法

js将汉字转为实体字符:

function convert2Entity(str) {var len = str.length;var re = [];<span>for</span> (var i = <span>0</span>; i < len; i++) {var code = str.charCodeAt(i); <span>if</span> (code > <span>256</span>) {re.push(<span>\'&#\'</span> + code + <span>\';\'</span>);} <span>else</span> {re.push(str.charAt(i));}}<span>return</span> re.join(<span>\'\'</span>);}php将实体字符转为utf-<span>8</span>汉字的方法:function entity2utf8onechar($unicode_c){$unicode_c_val = intval($unicode_c);$f=<span>0x80</span>; <span>//</span> <span>10000000</span>$str = <span>\"\"</span>;<span>//</span> U-<span>00000000</span> - U-<span>0000007</span>F: 0xxxxxxx<span>if</span>($unicode_c_val <= <span>0x7F</span>){ $str = <span>chr</span>($unicode_c_val); } //U-<span>000000</span>8<span>0</span> - U-<span>000007</span>FF: <span>110</span>xxxxx <span>10</span>xxxxxx <span>else</span> <span>if</span>($unicode_c_val >= <span>0x80</span> && $unicode_c_val <= <span>0x7FF</span>){ $h=<span>0xC0</span>; <span>//</span> <span>11000000</span> $c1 = $unicode_c_val >> <span>6</span> | $h;$c2 = ($unicode_c_val & <span>0x3F</span>) | $f;$str = <span>chr</span>($c1).chr($c2);}//U-<span>00000</span>80<span>0</span> - U-<span>0000</span>FFFF: <span>1110</span>xxxx <span>10</span>xxxxxx <span>10</span>xxxxxx<span>else</span> <span>if</span>($unicode_c_val >= <span>0x800</span> && $unicode_c_val <= <span>0xFFFF</span>){ $h=<span>0xE0</span>; <span>//</span> <span>11100000</span> $c1 = $unicode_c_val >> <span>12</span> | $h;$c2 = (($unicode_c_val & <span>0xFC0</span>) >> <span>6</span>) | $f;$c3 = ($unicode_c_val & <span>0x3F</span>) | $f;$str=<span>chr</span>($c1).chr($c2).chr($c3);}//U-<span>00010000</span> - U-<span>001</span>FFFFF: <span>11110</span>xxx <span>10</span>xxxxxx <span>10</span>xxxxxx <span>10</span>xxxxxx<span>else</span> <span>if</span>($unicode_c_val >= <span>0x10000</span> && $unicode_c_val <= <span>0x1FFFFF</span>){ $h=<span>0xF0</span>; <span>//</span> <span>11110000</span> $c1 = $unicode_c_val >> <span>18</span> | $h;$c2 = (($unicode_c_val & <span>0x3F000</span>) >><span>12</span>) | $f;$c3 = (($unicode_c_val & <span>0xFC0</span>) >><span>6</span>) | $f;$c4 = ($unicode_c_val & <span>0x3F</span>) | $f;$str = <span>chr</span>($c1).chr($c2).chr($c3).chr($c4);}//U-<span>00200000</span> - U-<span>03</span>FFFFFF: <span>111110</span>xx <span>10</span>xxxxxx <span>10</span>xxxxxx <span>10</span>xxxxxx <span>10</span>xxxxxx<span>else</span> <span>if</span>($unicode_c_val >= <span>0x200000</span> && $unicode_c_val <= <span>0x3FFFFFF</span>){ $h=<span>0xF8</span>; <span>//</span> <span>11111000</span> $c1 = $unicode_c_val >> <span>24</span> | $h;$c2 = (($unicode_c_val & <span>0xFC0000</span>)>><span>18</span>) | $f;$c3 = (($unicode_c_val & <span>0x3F000</span>) >><span>12</span>) | $f;$c4 = (($unicode_c_val & <span>0xFC0</span>) >><span>6</span>) | $f;$c5 = ($unicode_c_val & <span>0x3F</span>) | $f;$str = <span>chr</span>($c1).chr($c2).chr($c3).chr($c4).chr($c5);}//U-<span>04000000</span> - U-<span>7</span>FFFFFFF: <span>1111110</span><span>x</span> <span>10</span>xxxxxx <span>10</span>xxxxxx <span>10</span>xxxxxx <span>10</span>xxxxxx <span>10</span>xxxxxx<span>else</span> <span>if</span>($unicode_c_val >= <span>0x4000000</span> && $unicode_c_val <= <span>0x7FFFFFFF</span>){ $h=<span>0xFC</span>; <span>//</span> <span>11111100</span> $c1 = $unicode_c_val >> <span>30</span> | $h;$c2 = (($unicode_c_val & <span>0x3F000000</span>)>><span>24</span>) | $f;$c3 = (($unicode_c_val & <span>0xFC0000</span>)>><span>18</span>) | $f;$c4 = (($unicode_c_val & <span>0x3F000</span>) >><span>12</span>) | $f;$c5 = (($unicode_c_val & <span>0xFC0</span>) >><span>6</span>) | $f;$c6 = ($unicode_c_val & <span>0x3F</span>) | $f;$str = <span>chr</span>($c1).chr($c2).chr($c3).chr($c4).chr($c5).chr($c6);}<span>return</span> $str;}function entities2utf8($unicode_c){$unicode_c = preg_replace(<span>\"/\\&\\#([\\da-f]{5})\\;/es\"</span>, <span>\"entity2utf8onechar(\'\\\\1\')\"</span>, $unicode_c);<span>return</span> $unicode_c;}
本站部分文章来自网络或用户投稿,如无特殊说明或标注,均为本站原创发布。涉及资源下载的,本站旨在共享仅供大家学习与参考,如您想商用请获取官网版权,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
科技百科

自动关机设置是什么(设置win11系统定时关机方法)

2023-5-7 19:19:09

科技百科

电脑没有声音了怎么办(电脑麦克风没声音的解决方法)

2023-5-7 19:19:11

搜索