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, 250000, 500000, 750000, 999999, 1000000, 4294967295, 4294967296 ); echo sprintf("%10s %9s %5s %5s\n", 'num', 'str', 'day', 'cmp'); foreach ($vals as $val) { $enc = gdr_encode($val); $dec = gdr_decode($enc); if ($dec !== $val) { echo "ERROR: $val => $enc => $dec\n"; } else { $days = sprintf('%01.1f', $val/24.0); echo sprintf("%10d %9s %5s %5d\n", $dec, "\"$enc\"", $days, strlen($enc)+2-strlen($val)); } }
based on bbsCa
Output for 4.3.0 - 5.6.21, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
num str day cmp 0 ""  0.0 1 1 "B"  0.0 2 99 "BH"  4.1 2 100 "BI"  4.2 1 255 "C*"  10.6 1 256 "C+"  10.7 1 999 "K="  41.6 1 1000 "K>"  41.7 0 9999 "BQ!" 416.6 1 10000 "BQ#" 416.7 0 65535 "H'f" 2730.6 0 65536 "H'g" 2730.7 0 99999 "L-`" 4166.6 0 100000 "L-{" 4166.7 -1 250000 "dxk" 10416.7 -1 500000 "7G+" 20833.3 -1 750000 "{4Q" 31250.0 -1 999999 "BaNz" 41666.6 0 1000000 "BaN0" 41666.7 -1 4294967295 "7`7nL" 178956970.6 -3 4294967296 "7`7nM" 178956970.7 -3