views:

316

answers:

3

I want a script in PHP and c# which will generate id's like the following sites are doing:

Youtube http://www.youtube.com/watch?v=OdAE3cWlmHw is doing it like OdAE3cWlmHw

Bit.ly http://bit.ly/2pUswX doing it like 2pUswX

What will be the function to generate such type of unique id's in PHP and c#?

+4  A: 

See this or this or this or this or this.

Anton Gogolev
Thanks, this one helped me: http://stackoverflow.com/questions/1075409/creating-your-own-tinyurl
Prashant
+1  A: 

There is some codeproject code you can take a look at for the C# side of things here:

http://www.codeproject.com/KB/database/Alphanumaric_incriment.aspx?msg=1983998

If you are familiar with both PHP and C#, this will hopefully answer your question.

Sohnee
+2  A: 

Create an array (or string) holding the characters A-Z and numbers 0-9. Then write a loop that runs for the number of characters you want in your ID - e.g. 10 - for ($i = 0; $i < 10; $i++)

In that loop, get a random number between 0 and the length of your array/string holding A-Z, 0-9, look up the character at that position in the array/string and append it to your ID string.

This logic is the same for both PHP and C#, just the language syntax will be different.

Andy Shellam