[PHP]json_encode中文JSON_UNESCAPED_UNICODE在php5.3返回null

注意当json_encode中文的时候 , 默认是以unicode编码的 , 如果想变成中文需要增加参数JSON_UNESCAPED_UNICODE

但是JSON_UNESCAPED_UNICODE参数是php5.4以上才支持的

所以可以使用下面这段代码:

function json_encode2($array)
{
    if(version_compare(PHP_VERSION,'5.4.0','<')){
        $str = json_encode($array);
        $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
            return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
        },$str);
        return $str;
    }else{
        return json_encode($array, JSON_UNESCAPED_UNICODE);
    }
}