PHP URL Shortener


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



Copy this code and paste it in your HTML
  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. ?>

URL: http://prianto.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.