0

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#?

djmzfKnm
  • 26,053
  • 67
  • 163
  • 224

2 Answers2

2

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
  • 14,945
  • 1
  • 25
  • 41
1

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.

Fenton
  • 224,347
  • 65
  • 373
  • 385