Tag: PHP json

PHP4用のjson_decode

  • 第2パラメータがtrueの時と同じ連想配列形式で返す
    function json_decode($json)
    {
        $def = array(
            '/[\r\n]/' => '',
            '/([{},\[\]])/' => "$1\n",
            '/^([^:]+):/m' => '$1 =>',
            '/([{\[])/' => 'array(',
            '/([}\]])/' => ')',
            '/\\\([\/])/' => '$1',
            '/\\\u([0-9a-f]{4})/e' => "mb_convert_encoding(pack('V', hexdec('U$1')), 'UTF-8', 'UCS-4LE')"
        );
    
        $p = array_keys($def);
        $r = array_values($def);
        $s = preg_replace($p, $r, $json);
    
        eval('$res = ' . $s . ';');
    
        return $res;
    }

Last-modified: 2011-08-02 (火) 13:25:24 (2325d)