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; $cmp_min = cmp($min); $cmp_max = cmp($max); if ($cmp_min === $cmp_max) { return; } // binary search for edges if ($cmp_min+1 !== $cmp_max) { echo "ERROR: invalid search assumption: possibly missing edges: [$min => $cmp_min, $max => $cmp_max]\n"; } while ($min <= $max) { $mid = (int)(($min+$max)/2); $cmp_mid = cmp($mid); echo "trace: [$min => $cmp_min, $mid => $cmp_mid, $max=>$cmp_max]\n"; if ($cmp_min === $cmp_mid && $cmp_mid !== cmp($mid+1)) { $vals[] = $mid; $vals[] = $mid+1; return; } if ($min === $max) { return; } if ($cmp_min < $cmp_mid) { $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); 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), cmp($val)); } }
based on DpM05
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
trace: [10 => 1, 54 => 1, 99=>2] trace: [55 => 1, 77 => 1, 99=>2] trace: [78 => 1, 88 => 1, 99=>2] trace: [89 => 1, 94 => 2, 99=>2] trace: [89 => 1, 91 => 1, 93=>2] trace: [1000 => 0, 5499 => 0, 9999=>1] trace: [5500 => 0, 7749 => 0, 9999=>1] trace: [7750 => 0, 8874 => 1, 9999=>1] trace: [7750 => 0, 8311 => 0, 8873=>1] trace: [8312 => 0, 8592 => 1, 8873=>1] trace: [8312 => 0, 8451 => 0, 8591=>1] trace: [8452 => 0, 8521 => 1, 8591=>1] trace: [8452 => 0, 8486 => 1, 8520=>1] trace: [8452 => 0, 8468 => 1, 8485=>1] trace: [8452 => 0, 8459 => 0, 8467=>1] trace: [8460 => 0, 8463 => 0, 8467=>1] trace: [100000 => -1, 549999 => -1, 999999=>0] trace: [550000 => -1, 774999 => -1, 999999=>0] trace: [775000 => -1, 887499 => 0, 999999=>0] trace: [775000 => -1, 831249 => 0, 887498=>0] trace: [775000 => -1, 803124 => 0, 831248=>0] trace: [775000 => -1, 789061 => 0, 803123=>0] trace: [775000 => -1, 782030 => 0, 789060=>0] trace: [775000 => -1, 778514 => -1, 782029=>0] trace: [778515 => -1, 780272 => 0, 782029=>0] trace: [778515 => -1, 779393 => 0, 780271=>0] trace: [778515 => -1, 778953 => 0, 779392=>0] trace: [778515 => -1, 778733 => 0, 778952=>0] trace: [778515 => -1, 778623 => -1, 778732=>0] trace: [778624 => -1, 778678 => -1, 778732=>0] trace: [778679 => -1, 778705 => 0, 778732=>0] trace: [778679 => -1, 778691 => 0, 778704=>0] trace: [778679 => -1, 778684 => -1, 778690=>0] trace: [778685 => -1, 778687 => -1, 778690=>0] num str time cmp 0 "" 0h 1 1 "B" 1h 2 9 "J" 9h 2 10 "K" 10h 1 91 "~" 3.8d 1 92 "BA" 3.8d 2 99 "BH" 4.1d 2 100 "BI" 4.2d 1 999 "K=" 5.9w 1 1000 "K>" 6.0w 0 8463 "~~" 1.0y 0 8464 "BAA" 1.0y 1 9999 "BQ!" 1.1y 1 10000 "BQ#" 1.1y 0 99999 "L-`" 11.4y 0 100000 "L-{" 11.4y -1 778687 "~~~" 88.9y -1 778688 "BAAA" 88.9y 0 999999 "BaNz" 114.2y 0 1000000 "BaN0" 114.2y -1 9999999 "M;r7" 1141.6y -1