Short unique id in php

前端 未结 16 1000
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 17:34

I want to create a unique id but uniqid() is giving something like \'492607b0ee414\'. What i would like is something similar to what tinyurl gives:

16条回答
  •  没有蜡笔的小新
    2020-11-29 18:15

    Best Answer Yet: Smallest Unique "Hash Like" String Given Unique Database ID - PHP Solution, No Third Party Libraries Required.

    Here's the code:

     36 | $base == 10) {
          echo 'BASE must be in the range 2-9 or 11-36';
          exit;
       } // if
    
       // maximum character string is 36 characters
       $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
       // strip off excess characters (anything beyond $base)
       $charset = substr($charset, 0, $base);
    
       if (!ereg('(^[0-9]{1,50}$)', trim($decimal))) {
          $error['dec_input'] = 'Value must be a positive integer with < 50 digits';
          return false;
       } // if
    
       do {
          // get remainder after dividing by BASE
          $remainder = bcmod($decimal, $base);
    
          $char      = substr($charset, $remainder, 1);   // get CHAR from array
          $string    = "$char$string";                    // prepend to output
    
          //$decimal   = ($decimal - $remainder) / $base;
          $decimal   = bcdiv(bcsub($decimal, $remainder), $base);
    
       } while ($decimal > 0);
    
       return $string;
    
    }
    
    ?>
    

提交回复
热议问题