json中国語は符号化問題を解決し、二重引用符を除去し、単引用符を付ける方法である.

1075 ワード

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