Jump to content

[SOLVED] Generate Randomized Alphanumeric String


AndieB

Recommended Posts

Hi all gurus!

 

 

I am off trying to see if I can create a ticket reservation system, where a visitor should be able to reserve tickets and get a randomized unique alphanumeric string in uppercase letters with a total of 8 characters.

 

I do not know how to create a script that randomizes letters, from A-Z, and puts it into a string only 8 characters long. To compare it with already stored "randomized" in a database, I guess it is only to do a search in the database.

 

Anyone who has a good advice or script on how to generate what I would like to achieve?

Example:

GUTIIKLE

YYTESVOP

PUERBHGA

 

I guess you all know what I am after. :)

 

Thankful for any kind of help!

 

Sincerely,

Andreas

first make an array of all the letters/numbers you want to use.  (if people will be typeing this string at somepoint I like to leave out the letter O and the number zero as well as the number 1 and letters like I and lowercase L)

 

then do a loop with 8 passes.

 

each time through the loop use rand() to pick a character out of your array and ad it to a string.

  • 5 months later...

<pre>
<?php 
srand(time());
$pool = array_merge(
	range('a', 'z'),
	range('A', 'Z'),
	range(0, 9)
);
$pool_size = count($pool) - 1;
for ($i = 0; $i < 8; $i++) {
	echo $pool[rand(0, $pool_size)];
}
?>
</pre>

 

Thank you very much!!

This solved it!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.