How to Generate a Random Video id like Youtube in PHP



PHP


In this article, we show how to generate a random video id like youtube does in PHP.

Youtube generates random video ids such as, https://www.youtube.com/watch?v=FXSuEIMrPQk

After the ?v= is the random video id that youtube generates.

So, in case you didn't know or weren't aware, youtube generates an 11-digit video id every time a user uploads a video. This 11-digit video id is a unique identifier for the video.

How youtube does this is it uses base-64 encoding.

Base-64 encoding is a system that uses 64 possible characters for each of the 11 digits.

Youtube uses 11 digits. Therefore, there are 64 x 64 x 64 x 64 x 64 x 64 x 64 x 64 x 64 x 64 x 64, or 6411, which is equal to 73,786,976,294,838,206,464 possible combinations.

So which characters does youtube use?

So we know that youtube uses a base-64 encoding system. Which 64 characters does youtube use?

The 64 characters that youtube uses is 0-9 (10 combinations), lowercase alphabetical characters a-z (26 characters), uppercase characters A-Z (26 characters), a hyphen (-), and an underscore (_).

So the characters are 0-9, a-z, A-Z, -, and _

These are all the possible characters that youtube uses to create a video id.


Random video id:

kg5QtcVJiPI


Full youtube URL:

https://www.youtube.com/watch?v=kg5QtcVJiPI




So you can see above we have created a random video id, such as what you would find generated by youtube.

If you refresh the page, each time you do so, a new video id will be generated.

Again, with just 11 digits, we are able to generate 73,786,976,294,838,206,464 possible video ids.

11 digits can generate over 73 quintillion possible combinations with base-64 encoding.

So how can we generate a random 11-digit video id such as youtube does with PHP?

The following code below generates a random 11-digit video id, shown below.



So the following PHP code above is able to generate an 11-digit random string from the characters we have provided in the $characters variable. This $characters variable contains 0-9,a-z,A-Z, -, and _

Those are the 64 characters that youtube uses in order to make a video id.

We then create a variable named $result and set it equal to ''

We then have a for loop in which we put the for loop from 0 to 11, in order to generate an 11-digit string.

This string is selected randomly from the $characters variable, choosing any characters from 0-9,a-z,A-Z, -, or _

This $result variable now contains the random-generated 11-digit string.

To attach this to the youtube URL, the following line does this, shown below.



So now we have the full youtube URL with the randomly generated video id.

So youtube may do it slightly different. I'm sure they filter out certain words that are offensive so that it doesn't show up in the URL. However, this shows the mere basics with the PHP language.


Related Resources

How to Insert Images into a MySQL Database Using PHP

How to Insert Videos into a MySQL Database Using PHP

How to Insert Files into a MySQL Database Using PHP

How to Create a Video Manager Using PHP & MySQL



HTML Comment Box is loading comments...