php 如何将unicode转utf8
2022-12-02 09:01:16
76
{{single.collect_count}}

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

UNI-APP开发(仿饿了么)开发课程:进入学习

最近开发时遇到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);//更高版本的PHPecho preg_replace_callback("#\\\u([0-9a-f]+)#i",function($m){return iconv('UCS-2','UTF-8', pack('H4', $m[1]));},$str);
登录后复制

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

还可以用以下办法解决:

echo json_encode("汉字", JSON_UNESCAPED_UNICODE);
登录后复制

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

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

以上就是php 如何将unicode转utf8的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}