How to generate a youtube like video unique ID

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coldwinds
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2008 2:26 pm

How to generate a youtube like video unique ID

Post 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?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to generate a youtube like video unique ID

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How to generate a youtube like video unique ID

Post 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();
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply