3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function gdr_encode($val) { // all characters that json encode without escaping $chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !#$%&\'()*+,-.:;<=>?@[]^_`{|}~'; $base = strlen($chrs); $str = ''; while ($val > 0) { $str = $chrs[$val % $base] . $str; $val = (int)($val / $base); } return $str; } function gdr_decode($str) { // all characters that json encode without escaping $chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !#$%&\'()*+,-.:;<=>?@[]^_`{|}~'; $base = strlen($chrs); $map = array(); for ($i=0; $i<$base; $i++) { $map[$chrs[$i]] = $i; } $val = 0; for ($i=0; $i<strlen($str); $i++) { $val *= $base; $val += (int)$map[$str[$i]]; } return $val; } $vals = array(0, 1, 99, 100, 255, 256, 999, 1000, 9999, 10000, 65535, 65536, 99999, 100000, 500000, 999999, 1000000, 4294967295, 4294967296); foreach ($vals as $val) { $enc = gdr_encode($val); $dec = gdr_decode($enc); if ($dec !== $val) { echo "ERROR: $val => $enc => $dec\n"; } else { echo sprintf("%2d %7s %10d\n", strlen($enc)+2-strlen($val), "\"$enc\"", $dec);} }
based on B38sS
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to undefined function  strlen() in /in/YsJYp:38 Stack trace: #0 {main} thrown in /in/YsJYp on line 38
Process exited with code 255.
Output for 5.0.0 - 5.6.28
Fatal error: Call to undefined function  strlen() in /in/YsJYp on line 38
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function:  strlen() in /in/YsJYp on line 38
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function:  strlen() in /in/YsJYp on line 38
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function:  strlen() in /in/YsJYp on line 38