fork download
  1. <?php
  2. $char = array(2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z');
  3.  
  4. $str1 = "00101";
  5. $str2 = "00001";
  6. $str = sprintf("%05d",$str1) . sprintf("%05d",$str2);
  7. $val = ltrim($str, "0");
  8. $val = encode($val, $char);
  9. $val2 = decode($val, $char);
  10.  
  11. function encode($val, $char){
  12. $result = "";
  13. $base = count($char);
  14.  
  15. while($val > 0){
  16. $result = $char[ fmod($val, $base) ] . $result;
  17. $val = floor($val / $base);
  18. }
  19.  
  20. return ($result == "" ) ? 0 : $result;
  21. }
  22.  
  23. function decode($str, $char){
  24. $result = 0;
  25. $base = count($char);
  26. $table = array_flip($char);
  27. echo implode(",", $table) . "\n";
  28. $digit = array_reverse(preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY));
  29.  
  30. foreach($digit as $i => $value){
  31. if(!isset($table[$value])) return false;
  32. $result += pow($base, $i) * $table[$value];
  33. }
  34.  
  35. return $result;
  36. }
  37.  
Success #stdin #stdout 0s 82944KB
stdin
Standard input is empty
stdout
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30