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
https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442202
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
https://forums.phpfreaks.com/topic/86542-generate-random-ids/#findComment-442221
Share on other sites

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.