Posted By

prianto on 06/13/11


Tagged

url php shortener


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


PHP URL Shortener


 / Published in: PHP
 

URL: http://prianto.net

  1. <?php
  2. define('CHARS', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  3. function getIDFromURL ($string, $base = CHARS){
  4. $length = strlen($base);
  5. $size = strlen($string) - 1;
  6. $string = str_split($string);
  7. $out = strpos($base, array_pop($string));
  8. foreach($string as $i => $char){
  9. $out += strpos($base, $char) * pow($length, $size - $i);
  10. }
  11. return $out;
  12. }
  13. function getURLFromID ($integer, $base = CHARS){
  14. $length = strlen($base);
  15. while($integer > $length - 1){
  16. $out = $base[fmod($integer, $length)] . $out;
  17. $integer = floor( $integer / $length );
  18. }
  19. return $base[$integer] . $out;
  20. }
  21. ?>

Report this snippet  

You need to login to post a comment.