4

I have an ID column in a table which stores the row ID number (auto increment), For example 1, 2, 3. I want to generated a random and unique string which could contain only numbers, alphabets and dash (-) and underscore (_). The length of string should be 4-6, and it should be unique. Can someone help me how to generate? thanks.

sunjie
  • 2,023
  • 7
  • 28
  • 28
  • possible duplicate of [How to create a random string using PHP?](http://stackoverflow.com/questions/853813/how-to-create-a-random-string-using-php) – Roddy Jun 28 '11 at 09:16
  • What have you tried to resolve the problem? Where are you stuck? – Nico Haase Sep 13 '22 at 08:56

3 Answers3

2

Use this - base_convert(mt_rand(0x1D39D3E06400000, 0x41C21CB8E0FFFFFF), 10, 36), but check new value against db.

X10nD
  • 21,638
  • 45
  • 111
  • 152
0
function random_gen($length)
{
  $random= "";
  srand((double)microtime()*1000000);
  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $char_list .= "abcdefghijklmnopqrstuvwxyz";
  $char_list .= "1234567890-_";
  // Add the special characters to $char_list if needed

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

$random_string = random_gen(6); //This will return a random 6 character string

Above function will generate the unique string

Marcus Olsson
  • 2,485
  • 19
  • 35
gmhk
  • 15,598
  • 27
  • 89
  • 112
  • See my comment to the previous answer. You don't want to call `strlen` there, and you still have to guarantee uniqueness. This function only leverages probabilistic unlikeliness. – Zach Rattner Jun 28 '11 at 05:38
  • My question is if this always generates an unique number of you try this long enough. Okay not more than the total number of permutations.. – Alfred Jun 28 '11 at 05:39
  • 1
    It is still possible (albeit unlikely) that this function will return the same value twice. You should check the database with a `SELECT` statement to make sure the string is unique. – Zach Rattner Jun 28 '11 at 05:49
-1

Try this

function genRandomString($length) {
   $characters = ’0123456789abcdefghijklmnopqrstuvwxyz-_’;
   $string = ”;    
   for ($p = 0; $p < $length; $p++) {
      $string .= $characters[mt_rand(0, strlen($characters))];
    }
   return $string;
}

Call the function with the length you want

Balanivash
  • 6,709
  • 9
  • 32
  • 48
  • You're not going to want to call `strlen` every time you iterate over the `for` loop, since it's not going to change inside the loop. I'd recommend setting a variable to the output of `strlen`, and then use that inside the loop. Also, if you call this function inside of a `do { ... } while (keyExists($String));` block, you can guarantee uniqueness. The `keyExists` function would have to do a `SELECT` statement to see if any rows exist with the given string. – Zach Rattner Jun 28 '11 at 05:36
  • ya.. `strlen` is going to be the same and a variable can be used for it. To guarantee uniqueness, storing and checking the db is absolutely necessary. Thanks :) – Balanivash Jun 28 '11 at 05:42