Advertisement
cdsatrian

sort url

Oct 22nd, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
  2. {
  3.   $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4.   if ($passKey !== null)
  5.   {
  6.     /* Although this function's purpose is to just make the
  7.     * ID short - and not so much secure,
  8.     * with this patch by Simon Franz (http://blog.snaky.org/)
  9.     * you can optionally supply a password to make it harder
  10.     * to calculate the corresponding numeric ID */
  11.     for ($n = 0; $n<strlen($index); $n++)
  12.     {
  13.       $i[] = substr( $index,$n ,1);
  14.     }
  15.     $passhash = hash('sha256',$passKey);
  16.     $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512',$passKey) : $passhash;
  17.     for ($n=0; $n < strlen($index); $n++)
  18.     {
  19.       $p[] = substr($passhash, $n ,1);
  20.     }
  21.     array_multisort($p, SORT_DESC, $i);
  22.     $index = implode($i);
  23.   }
  24.   $base = strlen($index);
  25.   if ($to_num)
  26.   {
  27.     // Digital number <<-- alphabet letter code
  28.     $in = strrev($in);
  29.     $out = 0;
  30.     $len = strlen($in) - 1;
  31.     for ($t = 0; $t <= $len; $t++)
  32.     {
  33.       $bcpow = bcpow($base, $len - $t);
  34.       $out  = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
  35.     }
  36.     if (is_numeric($pad_up))
  37.     {
  38.       $pad_up--;
  39.       if ($pad_up > 0)
  40.       {
  41.         $out -= pow($base, $pad_up);
  42.       }
  43.     }
  44.     $out = sprintf('%F', $out);
  45.     $out = substr($out, 0, strpos($out, '.'));
  46.   }
  47.   else
  48.   {
  49.     // Digital number -->> alphabet letter code
  50.     if (is_numeric($pad_up))
  51.     {
  52.       $pad_up--;
  53.       if ($pad_up > 0)
  54.       {
  55.         $in += pow($base, $pad_up);
  56.       }
  57.     }
  58.     $out = "";
  59.     for ($t = floor(log($in, $base)); $t >= 0; $t--)
  60.     {
  61.       $bcp = bcpow($base, $t);
  62.       $a  = floor($in / $bcp) % $base;
  63.       $out = $out . substr($index, $a, 1);
  64.       $in = $in - ($a * $bcp);
  65.     }
  66.     $out = strrev($out); // reverse
  67.   }
  68.   return $out;
  69. }
  70. $r=rand();
  71. echo $r."<br>";
  72. $s=alphaID($r,false,false,'678%4@Ttuy');
  73. echo $s."<br>";
  74. echo alphaID($s,true,false,'678%4@Ttuy');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement