matriphe

Simple Hash Function to Create "Tiny URL" Slug

Aug 19th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.41 KB | None | 0 0
  1. <?php
  2. public function makehash($num)
  3. {
  4.     $alphabet = 'abcdefghijklmnopqrstuvwxyz_0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  5.     $base_count = strlen($alphabet);
  6.    
  7.     $encoded = '';
  8.    
  9.     while ($num >= $base_count)
  10.     {
  11.         $div = $num/$base_count;
  12.         $mod = ($num-($base_count*intval($div)));
  13.         $encoded .= $alphabet[$mod];
  14.         $num = intval($div);
  15.     }
  16.    
  17.     if ($num) $encoded .= $alphabet[$num];
  18.    
  19.     return $encoded;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment