Get string of random characters

There are times when you need to get a string of random characters of the specified length. It could be the default password for a new account or a unique identifier for something (you would probably be better off generating a guid) in your database.

Here is a function for those occasions. It generates a string of the specified length consisting of random characters. You can choose which type of characters to include - lowercase, uppercase, numeric or special.

  1. <?php
  2.  
  3. /**
  4.  * Generate string of random characters
  5.  *
  6.  * @param integer $length  Length of the string to generate
  7.  * @param integer $lower   Include lower case characters
  8.  * @param integer $upper   Include uppercase characters
  9.  * @param integer $nums    Include numbers
  10.  * @param integer $special Include special characters
  11.  * @return string
  12.  */
  13. function getRandomCharString($length, $lower = true, $upper = true, $nums = true, $special = false)
  14. {
  15.     $pool_lower = 'abcdefghijklmopqrstuvwxyz';
  16.     $pool_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  17.     $pool_nums = '0123456789';
  18.     $pool_special = '!$%^&*+#~/|';
  19.    
  20.     $pool = '';
  21.     $res = '';
  22.    
  23.     if ($lower === true) {
  24.         $pool .= $pool_lower;
  25.     }
  26.     if ($upper === true) {
  27.         $pool .= $pool_upper;
  28.     }
  29.     if ($nums === true) {
  30.         $pool .= $pool_nums;
  31.     }
  32.     if ($special === true) {
  33.         $pool .= $pool_special;
  34.     }
  35.    
  36.     if (($length < 0) || ($length == 0)) {
  37.         return $res;
  38.     }
  39.    
  40.     srand((double) microtime() * 1000000);
  41.    
  42.     for ($i = 0; $i < $length; $i++) {
  43.         $charidx = rand() % strlen($pool);
  44.         $char = substr($pool, $charidx, 1);
  45.         $res .= $char;
  46.     }
  47.    
  48.     return $res;
  49. }

The first thing we do is we define available lists of characters - lowercase, uppercase, special and numeric.

Next, we check, which types have to included and depending on the parameters we append them to the $pool variable.

After that, as a precaution, we make sure that the required length of the string to generate is at least one character. If it is zero or less, we simply return an empty string.

In order to initialize random number generator we call srand function. By doing that we ensure that the characters that we pick in the next step will random every time we do it.

And last we pick a random character out of our $pool string and append it to the end of $res string $length times. E.g. if it was set to 10, then we would get a 10 character long string.

The last line, as always, returns our result.

Enjoy!

Comments
No comments
Name
Email (required)
will not be published
Website
Recaptcha
you will only be required to fill it in once in this session

You can use [code][/code] tags in your comments