How to convert php unicode to Chinese characters

藏色散人
Release: 2023-03-01 21:50:02
Original
3221 people have browsed it

php unicode转汉字的方法:首先定义一个“unicode2Chinese”方法;然后在方法体内使用“preg_replace_callback”等函数实现转换逻辑;最后执行“unicode2Chinese”方法即可。

How to convert php unicode to Chinese characters

php中unicode转中文

php中,没有现成的unicode转中文方法。下面有个简易方法可以处理此种情况

function unicode2Chinese($str)
{
    return preg_replace_callback("#\\\u([0-9a-f]{4})#i",
        function ($r) {return iconv('UCS-2BE', 'UTF-8', pack('H4', $r[1]));},
        $str);
}
Copy after login

示例代码

 echo unicode2Chinese('\u53ea\u80fd\u4e2d\u6587\u624d\u61c2\u6211');
 echo "\n";
Copy after login

效果展示

[yangyu1@bw-dev-b-v01 16:56:35 ~]$ php unicode2Chinese.php
Copy after login

只能中文才懂我。

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

The above is the detailed content of How to convert php unicode to Chinese characters. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!