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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.