Home  >  Article  >  Backend Development  >  How to convert php unicode to Chinese characters

How to convert php unicode to Chinese characters

藏色散人
藏色散人Original
2020-07-04 09:50:593202browse

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);
}

示例代码

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

效果展示

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

只能中文才懂我。

更多相关知识,请访问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!

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