Page 1 of 1

How to generate a youtube like video unique ID

Posted: Sat Jun 21, 2008 2:35 pm
by coldwinds
For instance

http://www.youtube.com/watch?v=NVt4jOasujc

Such as 'NVt4jOasujc' instead of a regularly used integer ID.

Is it generated randomly or there is a special algorithm like having a connect with the auto increased ID?

Re: How to generate a youtube like video unique ID

Posted: Sat Jun 21, 2008 5:17 pm
by jaoudestudios
PHP has a built-in unique random number generator, which is really good as it ensures a unique number as it is based on the micro unix time stamp.

$unique_number = uniqid(rand(), true);

Hope this helps

Re: How to generate a youtube like video unique ID

Posted: Sat Jun 21, 2008 5:22 pm
by s.dot
Or you could create a recursive function.. probably not recommended, but it's what I do.

Code: Select all

function genRandId()
{
    $chars = array_merge(range('A', 'Z'), range(0, 9), array('-', '_'));
    shuffle($chars)
 
    $randid = $chars[0] . $chars[1] . $chars[2] . $chars[3] . $chars[4] . $chars[5] . $chars[6];
 
    if ($thisRandIdAlreadyExistsInDatabase)
    {
        genRandId();
    }
 
    return $randid;
}
 
echo genRandId();