Short unique id in php

前端 未结 16 993
没有蜡笔的小新
没有蜡笔的小新 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:18

    function rand_str($len = 12, $type = '111', $add = null) {
        $rand = ($type[0] == '1'  ? 'abcdefghijklmnpqrstuvwxyz' : '') .
                ($type[1] == '1'  ? 'ABCDEFGHIJKLMNPQRSTUVWXYZ' : '') .
                ($type[2] == '1'  ? '123456789'                 : '') .
                (strlen($add) > 0 ? $add                        : '');
    
        if(empty($rand)) $rand = sha1( uniqid(mt_rand(), true) . uniqid( uniqid(mt_rand(), true), true) );
    
        return substr(str_shuffle( str_repeat($rand, 2) ), 0, $len);
    }
    

提交回复
热议问题