How to convert unicode to utf8 in php

藏色散人
Release: 2023-03-02 15:58:02
Original
3051 people have browsed it

php将unicode转utf8的方法:首先创建PHP代码示例文件;然后使用“json_encode("汉字", JSON_UNESCAPED_UNICODE);”语句实现unicode转utf8即可。

How to convert unicode to utf8 in php

最近开发时遇到Unicode编码问题,找了半天才知道PHP并没有Unicode转码函数,终于发现用一行PHP代码解决的方案:

$str  = '{"success":true,"msg":"\u6d3b\u52a8\u63a5\u53e3"}';
echo $str= preg_replace("#\\\u([0-9a-f]+)#ie","iconv('UCS-2','UTF-8', pack('H4', '\\1'))",$str);
//更高版本的PHP
echo preg_replace_callback("#\\\u([0-9a-f]+)#i",function($m){return iconv('UCS-2','UTF-8', pack('H4', $m[1]));},$str);
Copy after login

非常强大的一行代码解决方法

还可以用以下办法解决:

echo json_encode("汉字", JSON_UNESCAPED_UNICODE);
Copy after login

 

PS:居然发现php有 Unicode_decode函数,可惜是实验性的。

更多相关知识,请访问PHP中文网

The above is the detailed content of How to convert unicode to utf8 in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template