PHP-解码unicode编码的中文字符

在Python中使用 "\uxxxx".decode("unicode_escape")

1.

class Helper_Tool {
    public static function unicodeDecode($data) {
        function replace_unicode_escape_sequence($match) {
            return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
        }  

        $rs = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $data);

        return $rs;
    }
}

$str = '{"name": "\u9a8c\u8bc1\u7801\u56fe\u7247\u9ad8\u5ea6\uff08\u5355\u4f4d\uff09"}';
echo Helper_Tool::unicodeDecode($str);

2.此方法字符串中不能有引号

function unicodeDecode($name){
    $json = '{"str":"'.$name.'"}';
    $arr = json_decode($json,, True);
    return empty($arr) ? '' : $arr['str'];
}

 

你可能感兴趣的:(PHP-解码unicode编码的中文字符)