3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function gdr_encode($val) { $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!='; $base = strlen($chars); $str = ''; while ($val > 0) { $str = $chars[$val % $base] . $str; $val = (int)($val / $base); } return $str; } function gdr_decode($str) { $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!='; $base = strlen($chars); $map = array(); for ($i=0; $i<$base; $i++) { $map[$chars[$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, 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 if (strlen($enc)+2 > strlen($val)) { echo "\"$enc\" $val WORSE\n"; } else if (strlen($enc)+2 == strlen($val)) { echo "\"$enc\" $val EQUAL\n"; } else { echo "\"$enc\" $val\n"; } }
based on cOgo7
Output for 4.3.0 - 7.1.0
"" 0 WORSE "B" 1 WORSE "Bj" 99 WORSE "Bk" 100 WORSE "D=" 255 WORSE "EA" 256 WORSE "Pn" 999 WORSE "Po" 1000 EQUAL "CcP" 9999 WORSE "CcQ" 10000 EQUAL "P==" 65535 EQUAL "QAA" 65536 EQUAL "Yaf" 99999 EQUAL "Yag" 100000 "D0I=" 999999 EQUAL "D0JA" 1000000 "D=====" 4294967295 "EAAAAA" 4294967296