Home >php教程 >php手册 >PHP将uncode转utf8,一行代码解决问题

PHP将uncode转utf8,一行代码解决问题

WBOY
WBOYOriginal
2016-06-06 19:52:551389browse

在很多场合能看到unicode编码过的文字,如\u6d3b\u52a8\u63a5\u53e3,虽然程序会认识,但人眼无法阅读,很不方便,网络上很多人写了很多的转换函数,但是一个比一个臃肿,终于发现用一行PHP代码解决的方案: $str = '{"success":true,"msg":"\u6d3b\u52a8\u6

在很多场合能看到unicode编码过的文字,如“\u6d3b\u52a8\u63a5\u53e3”,虽然程序会认识,但人眼无法阅读,很不方便,网络上很多人写了很多的转换函数,但是一个比一个臃肿,终于发现用一行PHP代码解决的方案:

<span>$str</span>  = '{"success":true,"msg":"\u6d3b\u52a8\u63a5\u53e3"}'<span>;
</span><span>echo</span> <span>$str</span>= <span>preg_replace</span>("#\\\u([0-9a-f]+)#ie","iconv('UCS-2','UTF-8', pack('H4', '\\1'))",<span>$str</span>);



如果服务端是自己写的,听说php5.4版本以上还可以用以下办法解决:

<span>echo</span> json_encode("汉字", JSON_UNESCAPED_UNICODE);  

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn