3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php if (phpversion() < '5.2.1') { die('php sucks'); } 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).' hour'; } if ($val < 24*10) { return sprintf('%1.2f', $val/24).' day '; } if ($val < 168*10) { return sprintf('%1.2f', $val/168).' week'; } if ($val < 730*10) { return sprintf('%1.2f', $val/730).' mnth'; } if ($val < 8760*100) { return sprintf('%1.2f', $val/8760).' year'; } if ($val < 876582*10) { return sprintf('%1.2f', $val/876582).' cent'; } return sprintf('%1.2f', $val/8.766e6).' mill'; } 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; } //$vals[] = $mid; // include visited values in results 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 %11s %4s\n", 'num', 'str', 'duration', '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 %11s %4d\n", $dec, "\"$enc\"", duration($val), cmp($val)); } }
based on 8qUbh
Output for 5.2.1 - 7.1.0
num str duration cmp 0 "" 0 hour 1 1 "B" 1 hour 2 9 "J" 9 hour 2 10 "K" 10 hour 1 91 "~" 3.79 day 1 92 "BA" 3.83 day 2 99 "BH" 4.12 day 2 100 "BI" 4.17 day 1 999 "K=" 5.95 week 1 1000 "K>" 5.95 week 0 8463 "~~" 0.97 year 0 8464 "BAA" 0.97 year 1 9999 "BQ!" 1.14 year 1 10000 "BQ#" 1.14 year 0 99999 "L-`" 11.42 year 0 100000 "L-{" 11.42 year -1 778687 "~~~" 88.89 year -1 778688 "BAAA" 88.89 year 0 999999 "BaNz" 1.14 cent 0 1000000 "BaN0" 1.14 cent -1 9999999 "M;r7" 1.14 mill -1
Output for 4.3.0 - 5.2.0
php sucks