Jump to content

Generate Random ID's


Dysan

Recommended Posts

Google found me:

 function randomPrefix($length)
{
$random= "";

srand((double)microtime()*1000000);

$data = "AbcDE123IJKLMN67QRSTUVWXYZ";
$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
$data .= "0FGH45OP89";

for($i = 0; $i < $length; $i++)
{
$random .= substr($data, (rand()%(strlen($data))), 1);
}

return $random;
}

randomPrefix(10); 

Link to comment
Share on other sites

This is a quick-n-dirty alternative I came up with:

 

<?php
$num = range(0,9); // 0-9
$ascii = range(65,90); // ASCII codes for A-Z
foreach ($ascii as $key => $value) {
  $char[$key] = chr($value);
}
$mixed = array_merge($num, $char);
$rand_keys = array_rand($mixed, 6);
foreach ($rand_keys as $value) {
  $charID .= $mixed[$value];
}
echo $charID;
?>

 

Note, this won't necessarily give you numbers AND alpha characters all the time, I tried it several times and twice there were all alpha's, and the others were a mix of both...

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.