Aidan's PHP Repository

A repository for PHP functions and classes ...

function.str_rand.php

Generate and return a random string

The default string returned is 8 alphanumeric characters.

The type of string returned can be changed with the "seeds" parameter. Four types are - by default - available: alpha, numeric, alphanum and hexidec.

If the "seeds" parameter does not match one of the above, then the string supplied is used.

  • Author: Aidan Lister <aidan@php.net>
  • Version: 2.1.0
  • Link: http://aidanlister.com/repos/v/function.str_rand.php
  • Views: 15112
  • Downloads: 976

Source

Download this script <?php
/**
 * Generate and return a random string
 *
 * The default string returned is 8 alphanumeric characters.
 *
 * The type of string returned can be changed with the "seeds" parameter.
 * Four types are - by default - available: alpha, numeric, alphanum and hexidec. 
 *
 * If the "seeds" parameter does not match one of the above, then the string
 * supplied is used.
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     2.1.0
 * @link        http://aidanlister.com/repos/v/function.str_rand.php
 * @param       int     $length  Length of string to be generated
 * @param       string  $seeds   Seeds string should be generated from
 */
function str_rand($length = 8, $seeds = 'alphanum')
{
    // Possible seeds
    $seedings['alpha']    = 'abcdefghijklmnopqrstuvwqyz';
    $seedings['numeric']  = '0123456789';
    $seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
    $seedings['hexidec']  = '0123456789abcdef';
 
    // Choose seed
    if (isset($seedings[$seeds])) {
        $seeds = $seedings[$seeds];
    }
 
    // Seed generator
    list($usec, $sec) = explode(' ', microtime());
    $seed = (float) $sec + ((float) $usec * 100000);
    mt_srand($seed);
 
    // Generate
    $str = '';
    $seeds_count = strlen($seeds);
    for ($i = 0; $length > $i; $i++) {
        $str .= $seeds{mt_rand(0, $seeds_count - 1)};
    }
 
    return $str;
}
 
?>

Example

<pre>
<?php
require_once 'function.str_rand.php';
 
// Simple
echo str_rand();
echo "\n";
 
// Longer
echo str_rand(15);
echo "\n";
 
// Really big number
echo str_rand(15, 'numeric');
echo "\n";
 
// Custom seeds
echo str_rand(15, '01');
?>
</pre>

Output

76sorzl9
gp1issw9yrzf1j2
205705873840795
001101101010010

Comments

February 27th, 2006
Verynice, tnx!
January 28th, 2006
Compact and useful. Keep it up
October 17th, 2005
This is great Aidan, but I think you should add A-Z to the alpha and alphanum list and add another arg which allows a user to select mix case, lower case, or upper case :)
May 13th, 2005
yeh, it's cool.. I'm gona use it
May 9th, 2005
GREAT :D
March 26th, 2005
klein aber oho ;) thx
March 21st, 2005
thanks. this is great!
February 10th, 2005
Thanks a lot! I used this to fix the auto-add function of firefox and IE, for my test-program.
February 4th, 2005
Very helpful. Thank you!
January 30th, 2005
Thanks much! I'm designing a mail-back registration feature for this tsunami disaster relief website I maintain. Need the number to generate random activation codes.
January 24th, 2005
Thanks, saved me some time!
January 20th, 2005
nice
January 19th, 2005
We just used that little bit in our app. I didnt want to build an array of random numbers casted as string, this made things fast.
January 11th, 2005
Awsome, thanks.
January 8th, 2005
I kiss you! :) Thanks a bunch!
January 6th, 2005
Perfect! Thank you
January 4th, 2005
Thanks! I'm using it at dailybeacon.utk.edu
December 9th, 2004
Thanks dood.
November 30th, 2004
Very cool! Saved me some work, thanks!
November 30th, 2004
That's it, saves me several minutes of work, will be included to my standards class
November 7th, 2004
Really great! Very flexible! Thanks alot!
November 5th, 2004
Just what I needed! Thanks.
November 5th, 2004
Gracias funciona realmente bien
November 5th, 2004
really simple and great solution, thanks.
November 5th, 2004
3 words: simple great usefull that is str_rand()
November 5th, 2004
I like it , thanks
November 5th, 2004
good!!! good job!! really tanks!!