Short unique id in php

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

    You can also do it like tihs:

    public static function generateCode($length = 6)
        {
            $az = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $azr = rand(0, 51);
            $azs = substr($az, $azr, 10);
            $stamp = hash('sha256', time());
            $mt = hash('sha256', mt_rand(5, 20));
            $alpha = hash('sha256', $azs);
            $hash = str_shuffle($stamp . $mt . $alpha);
            $code = ucfirst(substr($hash, $azr, $length));
            return $code;
        }
    

提交回复
热议问题