json中文解决编码问题,和去除双引号,加单引号方法。

/*json添加单引号*/
function my_arr_zhuan_json($arr)
{
    $jsonstr = $this->my_json_decode(json_encode($arr,true));

    $new_jsonstr = $this->decodeUnicode($jsonstr);

    $str1 = str_replace("name:","name:'",$new_jsonstr);

    $str2 = str_replace(",data","',data",$str1);

    return $str2;
}

/*json去除所有双引号*/
function my_json_decode($str) {        //       
    $str = preg_replace('#"(.*?)"#i', '$1$2', $str);   //去掉所有的双引号        
    return $str;    
}


/*json只去除key双引号*/
function my_json_decode($str) {        //       
    $str = preg_replace('/"(\w+)"(\s*:\s*)/is', '$1$2', $str);   //去掉key的双引号        
    return $str;    
} 

/*json解析中文*/   
function decodeUnicode($str)
{
    return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
        create_function(
            '$matches',
            'return mb_convert_encoding(pack("H*", $matches[1] ), "UTF-8", "UCS-2BE");'
        ),
        $str);
}

猜你喜欢

转载自blog.csdn.net/weixin_40896800/article/details/83088095
今日推荐