Posted By

localhorst on 06/13/09


Tagged

url short


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

luman
Balamir


Create short IDs with PHP - Like Youtube or TinyURL


 / Published in: PHP
 

URL: http://kevin.vanzonneveld.net/techblog/article/create_short_ids_with_php_like_youtube_or_tinyurl/

Example

Running:

alphaID(9007199254740989);

will return 'PpQXn7COf' and:

alphaID('PpQXn7COf', true);

will return '9007199254740989'

Easy right?

  1. <?php
  2. /**
  3.  * Translates a number to a short alhanumeric version
  4.  *
  5.  * Translated any number up to 9007199254740992
  6.  * to a shorter version in letters e.g.:
  7.  * 9007199254740989 --> PpQXn7COf
  8.  *
  9.  * specifiying the second argument true, it will
  10.  * translate back e.g.:
  11.  * PpQXn7COf --> 9007199254740989
  12.  *
  13.  * this function is based on any2dec && dec2any by
  14.  * fragmer[at]mail[dot]ru
  15.  * see: http://nl3.php.net/manual/en/function.base-convert.php#52450
  16.  *
  17.  * If you want the alphaID to be at least 3 letter long, use the
  18.  * $pad_up = 3 argument
  19.  *
  20.  * In most cases this is better than totally random ID generators
  21.  * because this can easily avoid duplicate ID's.
  22.  * For example if you correlate the alpha ID to an auto incrementing ID
  23.  * in your database, you're done.
  24.  *
  25.  * The reverse is done because it makes it slightly more cryptic,
  26.  * but it also makes it easier to spread lots of IDs in different
  27.  * directories on your filesystem. Example:
  28.  * $part1 = substr($alpha_id,0,1);
  29.  * $part2 = substr($alpha_id,1,1);
  30.  * $part3 = substr($alpha_id,2,strlen($alpha_id));
  31.  * $destindir = "/".$part1."/".$part2."/".$part3;
  32.  * // by reversing, directories are more evenly spread out. The
  33.  * // first 26 directories already occupy 26 main levels
  34.  *
  35.  * more info on limitation:
  36.  * - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372
  37.  *
  38.  * if you really need this for bigger numbers you probably have to look
  39.  * at things like: http://theserverpages.com/php/manual/en/ref.bc.php
  40.  * or: http://theserverpages.com/php/manual/en/ref.gmp.php
  41.  * but I haven't really dugg into this. If you have more info on those
  42.  * matters feel free to leave a comment.
  43.  *
  44.  * @author Kevin van Zonneveld <[email protected]>
  45.  * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  46.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
  47.  * @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
  48.  * @link http://kevin.vanzonneveld.net/
  49.  *
  50.  * @param mixed $in String or long input to translate
  51.  * @param boolean $to_num Reverses translation when true
  52.  * @param mixed $pad_up Number or boolean padds the result up to a specified length
  53.  *
  54.  * @return mixed string or long
  55.  */
  56. function alphaID($in, $to_num = false, $pad_up = false)
  57. {
  58. $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  59. $base = strlen($index);
  60.  
  61. if ($to_num) {
  62. // Digital number <<-- alphabet letter code
  63. $in = strrev($in);
  64. $out = 0;
  65. $len = strlen($in) - 1;
  66. for ($t = 0; $t <= $len; $t++) {
  67. $bcpow = bcpow($base, $len - $t);
  68. $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
  69. }
  70.  
  71. if (is_numeric($pad_up)) {
  72. $pad_up--;
  73. if ($pad_up > 0) {
  74. $out -= pow($base, $pad_up);
  75. }
  76. }
  77. } else {
  78. // Digital number -->> alphabet letter code
  79. if (is_numeric($pad_up)) {
  80. $pad_up--;
  81. if ($pad_up > 0) {
  82. $in += pow($base, $pad_up);
  83. }
  84. }
  85.  
  86. $out = "";
  87. for ($t = floor(log10($in) / log10($base)); $t >= 0; $t--) {
  88. $a = floor($in / bcpow($base, $t));
  89. $out = $out . substr($index, $a, 1);
  90. $in = $in - ($a * bcpow($base, $t));
  91. }
  92. $out = strrev($out); // reverse
  93. }
  94.  
  95. return $out;
  96. }
  97. ?>

Report this snippet  

You need to login to post a comment.