PHP把unicode编码的字符串转为人眼可看的字符串

json字符串里面,中文被unicode编码了,看不出来什么:

$s = '[{"param_name":"email","param_caption":"\u90ae\u7bb1","operator":"\u5305\u542b","value":"aaaa\u5927\u592b\u6492"}]';

用这个函数可以转回中文:

   /**
     * 把unicode编码的字符串转为人眼可看的字符串
     * @param $unicode_str
     *
     * @return string
     */
    function unicodeDecode($unicode_str){
        $unicode_str = str_replace('"', '\"', $unicode_str);
        $unicode_str = str_replace("'", "\'", $unicode_str);
        $json = '{"str":"'.$unicode_str.'"}';

        $arr = json_decode($json,true);

        if(empty($arr)){
            return '';
        }

        return $arr['str'];
    }

转换后:

[{"param_name":"email","param_caption":"邮箱","operator":"包含","value":"aaaa大夫撒"}]

猜你喜欢

转载自blog.csdn.net/wuzuyu365/article/details/82462770