Short URL function


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. // ---------------------------------------------------------------
  3. // Converts decimal to alphanumeric values
  4. // ---------------------------------------------------------------
  5. // 62 possibles per letter, counting from 1.
  6. // With 1 letter, 61 URLS
  7. // With 2 letters, 3,843 URLS
  8. // With 3 letters, 238,327 URLS
  9. // With 4 letters, 15,252,991 URLS
  10. // ---------------------------------------------------------------
  11.  
  12. $chrs = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S' ,'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  13.  
  14. function int_to_alph($int, $chrs)
  15. {
  16. $base = sizeof($chrs);
  17. do {
  18. $alph = $chrs[($int % $base)] . $alph;
  19. } while($int = intval($int / $base));
  20. return $alph;
  21. }
  22.  
  23. function alph_to_int($alph, $chrs)
  24. {
  25. $base = sizeof($chrs);
  26. for($i = 0, $int = 0; $i < strlen($alph); $i++)
  27. {
  28. $int += intval(array_search(substr($alph, strlen($alph) - $i - 1, 1), $chrs)) * pow($base, $i);
  29. }
  30. return $int;
  31. }
  32. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.