PHP教程:如何将JSON Unicode转换为中文字符
2024-05-16

function unicode_to_chinese($str) {
    return preg_replace_callback('/\u([0-9a-fA-F]{4})/', function($matches) {
        return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UCS-2BE');
    }, $str);
}

// 示例用法
$json_data = '{"name":"u4e2du6587"}';
$data = json_decode($json_data, true);
$data['name'] = unicode_to_chinese($data['name']);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
登录后复制