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; } function duration($val) { if ($val < 24) { return sprintf('%d', $val).'h'; } if ($val < 24*10) { return sprintf('%1.1f', $val/24).'d'; } if ($val < 168*10) { return sprintf('%1.1f', $val/168).'w'; } //if ($val < 730*10) { // return sprintf('%1.1f', $val/730).'m'; //} return sprintf('%1.1f', $val/8760).'y'; } function cmp($val) { return strlen(gdr_encode($val))+2-strlen($val); } function insert(&$vals, $n) { $min = intval('1'.str_repeat('0', $n-1)); $max = intval(str_repeat('9', $n)); $vals[] = $min; $vals[] = $max; $len = cmp($min); if ($len !== cmp($max)) { return; } while ($min < $max) { $mid = round(($min+$max)/2, 0)-1; $k = cmp($mid); echo "$min $mid $max $k\n"; if ($k == $len && $k < cmp($mid+1)) { $vals[] = $mid; $vals[] = $mid+1; return; } if ($k <= $len) { $max = $mid-1; } else { $min = $mid+1; } } } $vals = array(0); for ($i=1; $i<8; $i++) { insert($vals, $i); } $vals = array_unique($vals); sort($vals); //$vals = array( // 0, 1, 99, 100, 255, 256, 999, 1000, 9999, 10000, 65535, 65536, 99999, // 100000, 250000, 500000, 750000, 999999, 1000000, 9999999, //4294967295, 4294967296 //); echo sprintf("%7s %7s %7s %4s\n", 'num', 'str', 'time', 'cmp'); foreach ($vals as $val) { $enc = gdr_encode($val); $dec = gdr_decode($enc); if ($dec !== $val) { echo "ERROR: $val => $enc => $dec\n"; } else { echo sprintf("%7d %7s %7s %4d\n", $dec, "\"$enc\"", duration($val), strlen($enc)+2-strlen($val)); } }
based on 2UCnb
Output for 4.3.0 - 7.1.0
1 4 9 2 1 1 3 2 100 549 999 1 100 323 548 1 100 210 322 1 100 154 209 1 100 126 153 1 100 112 125 1 100 105 111 1 100 101 104 1 10000 54999 99999 0 10000 32498 54998 0 10000 21248 32497 0 10000 15623 21247 0 10000 12810 15622 0 10000 11404 12809 0 10000 10701 11403 0 10000 10349 10700 0 10000 10173 10348 0 10000 10085 10172 0 10000 10041 10084 0 10000 10019 10040 0 10000 10008 10018 0 10000 10003 10007 0 10000 10000 10002 0 1000000 5499999 9999999 -1 1000000 3249998 5499998 -1 1000000 2124998 3249997 -1 1000000 1562498 2124997 -1 1000000 1281248 1562497 -1 1000000 1140623 1281247 -1 1000000 1070310 1140622 -1 1000000 1035154 1070309 -1 1000000 1017576 1035153 -1 1000000 1008787 1017575 -1 1000000 1004392 1008786 -1 1000000 1002195 1004391 -1 1000000 1001096 1002194 -1 1000000 1000547 1001095 -1 1000000 1000272 1000546 -1 1000000 1000135 1000271 -1 1000000 1000066 1000134 -1 1000000 1000032 1000065 -1 1000000 1000015 1000031 -1 1000000 1000006 1000014 -1 1000000 1000002 1000005 -1 1000000 1000000 1000001 -1 num str time cmp 0 "" 0h 1 1 "B" 1h 2 9 "J" 9h 2 10 "K" 10h 1 99 "BH" 4.1d 2 100 "BI" 4.2d 1 999 "K=" 5.9w 1 1000 "K>" 6.0w 0 9999 "BQ!" 1.1y 1 10000 "BQ#" 1.1y 0 99999 "L-`" 11.4y 0 100000 "L-{" 11.4y -1 999999 "BaNz" 114.2y 0 1000000 "BaN0" 114.2y -1 9999999 "M;r7" 1141.6y -1