fork(4) download
  1. <?php
  2. /**
  3.  * Base62: A class to convert a number from any base between 2-62 to any other base between 2-62
  4.  * It doesn't use BC Math functions so works without the use of BC Math library.
  5.  * It uses the native base_convert functions when the base is below 36 for faster execution.
  6.  * The output number is backward compatible with the native base_convert function.
  7.  *
  8.  * Author : Lalit Patel
  9.  * Website: http://w...content-available-to-author-only...t.org/lab/base62-php-convert-number-to-base-62-for-short-urls
  10.  * License: Apache License 2.0
  11.  * http://w...content-available-to-author-only...e.org/licenses/LICENSE-2.0
  12.  * Version: 0.1 (08 December 2011)
  13.  *
  14.  * Usage:
  15.  * $converted_num = Base62::convert($number, $from_base, $to_base);
  16.  */
  17.  
  18. class Base62 {
  19. /**
  20.   * Converts a number/string from any base between 2-62 to any other base from 2-62
  21.   * @param mixed $number
  22.   * @param integer $from_base
  23.   * @param integer $to_base
  24.   * @return $conveted_number.
  25.   */
  26. public static function convert($number, $from_base=10, $to_base=62) {
  27. if($to_base > 62 || $to_base < 2) {
  28. trigger_error("Invalid base (".he($to_base)."). Max base can be 62. Min base can be 2.", E_USER_ERROR);
  29. }
  30. //OPTIMIZATION: no need to convert 0
  31. if("{$number}" === '0') {
  32. return 0;
  33. }
  34.  
  35. //OPTIMIZATION: if to and from base are same.
  36. if($from_base == $to_base){
  37. return $number;
  38. }
  39.  
  40. //OPTIMIZATION: if base is lower than 36, use PHP internal function
  41. if($from_base <= 36 && $to_base <= 36) {
  42. // for lower base, use the default PHP function for faster results
  43. return base_convert($number, $from_base, $to_base);
  44. }
  45.  
  46. // char list starts from 0-9 and then small alphabets and then capital alphabets
  47. // to make it compatible with eixisting base_convert function
  48. $charlist = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  49. if($from_base < $to_base) {
  50. // if converstion is from lower base to higher base
  51. // first get the number into decimal and then convert it to higher base from decimal;
  52.  
  53. if($from_base != 10){
  54. $decimal = self::convert($number, $from_base, 10);
  55. } else {
  56. $decimal = intval($number);
  57. }
  58.  
  59. //get the list of valid characters
  60. $charlist = substr($charlist, 0, $to_base);
  61.  
  62. if($number == 0) {
  63. return 0;
  64. }
  65. $converted = '';
  66. while($number > 0) {
  67. $converted = $charlist{($number % $to_base)} . $converted;
  68. $number = floor($number / $to_base);
  69. }
  70. return $converted;
  71. } else {
  72. // if conversion is from higher base to lower base;
  73. // first convert it into decimal and the convert it to lower base with help of same function.
  74. $number = "{$number}";
  75. $length = strlen($number);
  76. $decimal = 0;
  77. $i = 0;
  78. while($length > 0) {
  79. $char = $number{$length-1};
  80. $pos = strpos($charlist, $char);
  81. if($pos === false){
  82. trigger_error("Invalid character in the input number: ".($char), E_USER_ERROR);
  83. }
  84. $decimal += $pos * pow($from_base, $i);
  85. $length --;
  86. $i++;
  87. }
  88. return self::convert($decimal, 10, $to_base);
  89. }
  90. }
  91. }
  92. echo Base62::convert(100)."\n";
  93. echo Base62::convert(1000)."\n";
  94. echo Base62::convert(10000)."\n";
  95. echo Base62::convert(100000)."\n";
  96. echo Base62::convert(1000000)."\n";
  97. echo Base62::convert(10000000)."\n";
  98. echo Base62::convert(100000000)."\n";
  99. echo Base62::convert(123456789089898)."\n";
  100.  
  101. ?>
  102.  
  103.  
Success #stdin #stdout 0.02s 13064KB
stdin
Standard input is empty
stdout
1C
g8
2Bi
q0U
4c92
FXsk
6LAze
z3wBX5