3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* simplejson - a tiny JSON parser for older PHP versions ------------ The main purpose of this is to allow the parsing of JSON encoded strings into PHP native structures, or PHP objects encoding into JSON strings. Primary target for this are mature systems running versions of PHP older than 5.2, which provides this functionality. The functions are confirmed to work on PHP as old as 4.1.2. The functions do not care about character encoding and will do nothing to magically fix character set issues. They'll work with the data as-provided and won't, for example, (un)escape \u0000 or \x00 characters. WARNING: Be aware that the string input is being "evaluated" and run by this function with all the implications that includes! ------------ Copyright (C) 2006 Borgar Thorsteinsson [borgar.undraland.com] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------ */ /** * Parses a JSON string into a PHP variable. * @param string $json The JSON string to be parsed. * @param bool $assoc Optional flag to force all objects into associative arrays. * @return mixed Parsed structure as object or array, or null on parser failure. */ function fromJSON ( $json, $assoc = false ) { /* by default we don't tolerate ' as string delimiters if you need this, then simply change the comments on the following lines: */ // $matchString = '/(".*?(?<!\\\\)"|\'.*?(?<!\\\\)\')/'; $matchString = '/".*?(?<!\\\\)"/'; // safety / validity test $t = preg_replace( $matchString, '', $json ); $t = preg_replace( '/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $t ); if ($t != '') { return null; } // build to/from hashes for all strings in the structure $s2m = array(); $m2s = array(); preg_match_all( $matchString, $json, $m ); foreach ($m[0] as $s) { $hash = '"' . md5( $s ) . '"'; $s2m[$s] = $hash; $m2s[$hash] = str_replace( '$', '\$', $s ); // prevent $ magic } // hide the strings $json = strtr( $json, $s2m ); // convert JS notation to PHP notation $a = ($assoc) ? '' : '(object) '; $json = strtr( $json, array( ':' => '=>', '[' => 'array(', '{' => "{$a}array(", ']' => ')', '}' => ')' ) ); // remove leading zeros to prevent incorrect type casting $json = preg_replace( '~([\s\(,>])(-?)0~', '$1$2', $json ); // return the strings $json = strtr( $json, $m2s ); /* "eval" string and return results. As there is no try statement in PHP4, the trick here is to suppress any parser errors while a function is built and then run the function if it got made. */ $f = @create_function( '', "return {$json};" ); $r = ($f) ? $f() : null; // free mem (shouldn't really be needed, but it's polite) unset( $s2m ); unset( $m2s ); unset( $f ); return $r; } /** * Encodes a PHP variable into a JSON string. * @param mixed $value A PHP variable to be encoded. */ function toJSON ( $value ) { if ($value === null) { return 'null'; }; // gettype fails on null? $out = ''; $esc = "\"\\/\n\r\t" . chr( 8 ) . chr( 12 ); // escaped chars $l = '.'; // decimal point switch ( gettype( $value ) ) { case 'boolean': $out .= $value ? 'true' : 'false'; break; case 'float': case 'double': // PHP uses the decimal point of the current locale but JSON expects %x2E $l = localeconv(); $l = $l['decimal_point']; // fallthrough... case 'integer': $out .= str_replace( $l, '.', $value ); // what, no getlocale? break; case 'array': // if array only has numeric keys, and is sequential... ? for ($i = 0; ($i < count( $value ) && isset( $value[$i]) ); $i++); if ($i === count($value)) { // it's a "true" array... or close enough $out .= '[' . implode(',', array_map('toJSON', $value)) . ']'; break; } // fallthrough to object for associative arrays... case 'object': $arr = is_object($value) ? get_object_vars($value) : $value; $b = array(); foreach ($arr as $k => $v) { $b[] = '"' . addcslashes($k, $esc) . '":' . toJSON($v); } $out .= '{' . implode( ',', $b ) . '}'; break; default: // anything else is treated as a string return '"' . addcslashes($value, $esc) . '"'; break; } return $out; } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0110.00718.28
8.3.50.0110.00921.94
8.3.40.0120.00618.68
8.3.30.0140.00019.10
8.3.20.0040.00420.38
8.3.10.0050.00323.65
8.3.00.0040.00422.25
8.2.180.0180.00418.28
8.2.170.0140.00022.96
8.2.160.0080.00622.06
8.2.150.0120.00324.18
8.2.140.0080.00024.66
8.2.130.0050.00326.16
8.2.120.0050.00317.36
8.2.110.0040.00422.16
8.2.100.0040.01117.42
8.2.90.0000.00719.05
8.2.80.0040.00417.97
8.2.70.0040.00417.38
8.2.60.0040.00417.80
8.2.50.0080.00018.07
8.2.40.0060.00317.97
8.2.30.0000.00817.85
8.2.20.0020.00517.53
8.2.10.0000.00819.27
8.2.00.0040.00417.47
8.1.280.0140.00725.92
8.1.270.0040.00423.84
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0060.00323.84
8.1.230.0070.00419.04
8.1.220.0040.00417.74
8.1.210.0000.00818.77
8.1.200.0060.00317.23
8.1.190.0080.00017.41
8.1.180.0080.00018.10
8.1.170.0090.00018.46
8.1.160.0000.00721.87
8.1.150.0000.00718.43
8.1.140.0000.00817.27
8.1.130.0030.00317.76
8.1.120.0040.00417.41
8.1.110.0040.00417.34
8.1.100.0030.00617.27
8.1.90.0040.00417.39
8.1.80.0040.00417.39
8.1.70.0030.00317.35
8.1.60.0030.00517.51
8.1.50.0000.00817.46
8.1.40.0030.00617.47
8.1.30.0000.00817.43
8.1.20.0040.00417.61
8.1.10.0000.00817.30
8.1.00.0000.00817.41
8.0.300.0000.00818.77
8.0.290.0040.00417.16
8.0.280.0030.00318.30
8.0.270.0040.00417.18
8.0.260.0070.00316.70
8.0.250.0030.00316.96
8.0.240.0000.00816.87
8.0.230.0000.00716.94
8.0.220.0040.00416.81
8.0.210.0000.00716.70
8.0.200.0060.00016.75
8.0.190.0030.00616.77
8.0.180.0090.00016.73
8.0.170.0000.00816.82
8.0.160.0020.00516.87
8.0.150.0000.00716.59
8.0.140.0060.00316.66
8.0.130.0060.00013.17
8.0.120.0030.00516.73
8.0.110.0000.00716.75
8.0.100.0000.00816.73
8.0.90.0020.00516.69
8.0.80.0030.01316.75
8.0.70.0040.00416.88
8.0.60.0000.00716.68
8.0.50.0000.00716.83
8.0.30.0050.01416.95
8.0.20.0120.00717.40
8.0.10.0000.00716.71
8.0.00.0130.00816.59
7.4.330.0000.00515.02
7.4.320.0060.00016.38
7.4.300.0030.00316.41
7.4.290.0000.00816.43
7.4.280.0000.00716.43
7.4.270.0040.00416.33
7.4.260.0000.00613.09
7.4.250.0030.00316.34
7.4.240.0050.00316.36
7.4.230.0030.00316.63
7.4.220.0060.01516.48
7.4.210.0140.00516.44
7.4.200.0000.00716.34
7.4.190.0000.00816.42
7.4.160.0090.00616.29
7.4.150.0070.01117.40
7.4.140.0110.00717.86
7.4.130.0070.01016.50
7.4.120.0080.01016.38
7.4.110.0080.00816.27
7.4.100.0150.00616.29
7.4.90.0130.00316.41
7.4.80.0080.01216.64
7.4.70.0000.01616.58
7.4.60.0030.01316.26
7.4.50.0020.00216.20
7.4.40.0080.00422.77
7.4.30.0080.01116.52
7.4.00.0150.00014.74
7.3.330.0060.00013.17
7.3.320.0000.00513.22
7.3.310.0030.00316.32
7.3.300.0000.00716.15
7.3.290.0070.00716.15
7.3.280.0030.01316.24
7.3.270.0040.01417.40
7.3.260.0120.00616.23
7.3.250.0120.00916.45
7.3.240.0130.00316.46
7.3.230.0090.00616.39
7.3.210.0110.00816.36
7.3.200.0140.00719.39
7.3.190.0100.00716.14
7.3.180.0000.01616.34
7.3.170.0090.00616.50
7.3.160.0080.00816.19
7.3.120.0070.00714.75
7.3.10.0000.01016.64
7.3.00.0000.01116.52
7.2.330.0150.00616.25
7.2.320.0100.01316.52
7.2.310.0090.00916.50
7.2.300.0130.00316.43
7.2.290.0060.00916.34
7.2.130.0040.01116.71
7.2.120.0080.00816.45
7.2.110.0070.00416.65
7.2.100.0030.00916.36
7.2.90.0100.00316.45
7.2.80.0090.00616.27
7.2.70.0090.00616.55
7.2.60.0000.01316.59
7.2.50.0060.00316.57
7.2.40.0060.00616.66
7.2.30.0140.00316.88
7.2.20.0070.00716.55
7.2.10.0000.00916.46
7.2.00.0040.01117.92
7.1.250.0000.00715.50
7.1.100.0060.00617.66
7.1.70.0050.00516.91
7.1.60.0130.01019.24
7.1.50.0060.00616.91
7.1.00.0070.06322.43
7.0.200.0030.00616.68
7.0.140.0000.08021.98
7.0.70.0130.04321.66
7.0.60.0100.04321.75
7.0.50.0000.05722.14
7.0.40.0070.05720.13
7.0.30.0030.09020.09
7.0.20.0070.09020.13
7.0.10.0030.05719.95
7.0.00.0070.04320.04
5.6.280.0130.06321.13
5.6.220.0100.07720.55
5.6.210.0070.07020.52
5.6.200.0030.06321.19
5.6.190.0000.09320.96
5.6.180.0030.07721.11
5.6.170.0030.07321.03
5.6.160.0030.06321.10
5.6.150.0070.04720.99
5.6.140.0130.04321.04
5.6.130.0030.07721.13
5.6.120.0170.07320.98
5.6.110.0070.05321.11
5.6.100.0100.08321.07
5.6.90.0100.07021.01
5.6.80.0100.06020.46
5.6.70.0070.05320.38
5.6.60.0100.08020.46
5.6.50.0070.07720.59
5.6.40.0100.07320.55
5.6.30.0030.08720.32
5.6.20.0100.08020.44
5.6.10.0030.08720.52
5.6.00.0070.04320.45
5.5.360.0100.08020.37
5.5.350.0000.06720.44
5.5.340.0030.08320.76
5.5.330.0100.05720.75
5.5.320.0200.07320.81
5.5.310.0000.08320.85
5.5.300.0030.08320.86
5.5.290.0100.08320.86
5.5.280.0070.08320.79
5.5.270.0100.06020.90
5.5.260.0030.09020.75
5.5.250.0000.05020.64
5.5.240.0070.07020.29
5.5.230.0100.07720.33
5.5.220.0030.08320.18
5.5.210.0030.07320.21
5.5.200.0130.06720.30
5.5.190.0070.03720.27
5.5.180.0130.07020.31
5.5.160.0070.07020.30
5.5.150.0100.06320.28
5.5.140.0070.08020.28
5.5.130.0000.04320.29
5.5.120.0000.06020.16
5.5.110.0030.07020.14
5.5.100.0070.05020.15
5.5.90.0000.06020.14
5.5.80.0030.08320.17
5.5.70.0030.08020.11
5.5.60.0100.07320.04
5.5.50.0030.08320.09
5.5.40.0030.08020.08
5.5.30.0000.08320.10
5.5.20.0100.04320.13
5.5.10.0030.04020.03
5.5.00.0170.02719.99
5.4.450.0030.08719.54
5.4.440.0000.08719.49
5.4.430.0030.04719.45
5.4.420.0000.04319.37
5.4.410.0100.07019.10
5.4.400.0070.07318.92
5.4.390.0100.07018.87
5.4.380.0070.07019.14
5.4.370.0070.07319.12
5.4.360.0030.08019.05
5.4.350.0030.06019.03
5.4.340.0030.07719.16
5.4.320.0070.04719.14
5.4.310.0030.07719.21
5.4.300.0000.08319.12
5.4.290.0000.05719.14
5.4.280.0100.07319.21
5.4.270.0030.06318.89
5.4.260.0100.06018.95
5.4.250.0000.08019.20
5.4.240.0000.05719.23
5.4.230.0030.07719.12
5.4.220.0100.07719.12
5.4.210.0100.07018.89
5.4.200.0070.08019.20
5.4.190.0030.05319.11
5.4.180.0000.04319.07
5.4.170.0030.04319.02
5.4.160.0030.03719.07
5.4.150.0000.04019.01
5.4.140.0000.04716.52
5.4.130.0000.04016.46
5.4.120.0070.03316.46
5.4.110.0030.03716.53
5.4.100.0000.04016.33
5.4.90.0030.05316.44
5.4.80.0100.05016.36
5.4.70.0030.03316.54
5.4.60.0170.05716.34
5.4.50.0070.03716.41
5.4.40.0100.04316.45
5.4.30.0130.06716.47
5.4.20.0000.03716.30
5.4.10.0000.03716.43
5.4.00.0030.04715.66
5.3.290.0030.05714.79
5.3.280.0000.04714.64
5.3.270.0030.03714.59
5.3.260.0030.06314.57
5.3.250.0070.04014.64
5.3.240.0030.03714.61
5.3.230.0030.04014.55
5.3.220.0100.05014.60
5.3.210.0070.03714.60
5.3.200.0100.03314.52
5.3.190.0000.04314.53
5.3.180.0030.04014.67
5.3.170.0000.06714.52
5.3.160.0070.03714.71
5.3.150.0000.04014.57
5.3.140.0000.04014.71
5.3.130.0030.04014.61
5.3.120.0100.05014.60
5.3.110.0000.04014.48
5.3.100.0030.06714.04
5.3.90.0000.04714.00
5.3.80.0000.03714.08
5.3.70.0000.03714.08
5.3.60.0030.03714.12
5.3.50.0070.03014.07
5.3.40.0030.03313.98
5.3.30.0030.03313.95
5.3.20.0030.03713.71
5.3.10.0000.03713.75
5.3.00.0030.03713.56
5.2.170.0000.03012.25
5.2.160.0030.03712.25
5.2.150.0030.04712.25
5.2.140.0030.03012.25
5.2.130.0100.02012.25
5.2.120.0000.03012.25
5.2.110.0000.04012.25
5.2.100.0070.05312.25
5.2.90.0000.03312.25
5.2.80.0000.03312.25
5.2.70.0000.04312.25
5.2.60.0030.05312.25
5.2.50.0100.03012.25
5.2.40.0000.03012.25
5.2.30.0000.04012.25
5.2.20.0070.05012.25
5.2.10.0030.02712.25
5.2.00.0030.03012.25
5.1.60.0030.02712.25
5.1.50.0030.02312.25
5.1.40.0000.03312.25
5.1.30.0070.02312.25
5.1.20.0000.02712.25
5.1.10.0030.02312.25
5.1.00.0030.02312.25
5.0.50.0000.02712.25
5.0.40.0000.02012.25
5.0.30.0000.03012.25
5.0.20.0000.02012.25
5.0.10.0000.03312.25
5.0.00.0000.06012.25
4.4.90.0000.01712.25
4.4.80.0000.01312.25
4.4.70.0000.01712.25
4.4.60.0000.01712.25
4.4.50.0000.01312.25
4.4.40.0000.02312.25
4.4.30.0000.01712.25
4.4.20.0000.01712.25
4.4.10.0000.02012.25
4.4.00.0000.02712.25
4.3.110.0030.01312.25
4.3.100.0030.01312.25
4.3.90.0000.01712.25
4.3.80.0070.05012.25
4.3.70.0000.03012.25
4.3.60.0000.02012.25
4.3.50.0000.02712.25
4.3.40.0070.03712.25
4.3.30.0000.02312.25
4.3.20.0000.02312.25
4.3.10.0000.01712.25
4.3.00.0070.03012.25

preferences:
50.98 ms | 401 KiB | 5 Q