[PHP]json_encode Chinese JSON_UNESCAPED_UNICODE returns null in php5.3

Note that when json_encode Chinese, the default is unicode encoding, if you want to become Chinese, you need to increase the parameter JSON_UNESCAPED_UNICODE

But the JSON_UNESCAPED_UNICODE parameter is only supported above php5.4

So you can use the following code:

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

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *