Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP: Generate random string
/**
* Generate and return a random characters string
*
* Useful for generating passwords or hashes.
*
* The default string returned is 8 alphanumeric characters string.
*
* The type of string returned can be changed with the "type" parameter.
* Seven types are - by default - available: basic, alpha, alphanum, num, nozero, unique and md5.
*
* @param string $type Type of random string. basic, alpha, alphanum, num, nozero, unique and md5.
* @param integer $length Length of the string to be generated, Default: 8 characters long.
* @return string
*/
function random_str($type = 'alphanum', $length = 8)
{
switch($type)
{
case 'basic' : return mt_rand();
break;
case 'alpha' :
case 'alphanum' :
case 'num' :
case 'nozero' :
$seedings = array();
$seedings['alpha'] = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$seedings['alphanum'] = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$seedings['num'] = '0123456789';
$seedings['nozero'] = '123456789';
$pool = $seedings[$type];
$str = '';
for ($i=0; $i < $length; $i++)
{
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
return $str;
break;
case 'unique' :
case 'md5' :
return md5(uniqid(mt_rand()));
break;
}
}
/*======================================================+
Usage Example:
---------------
# Generate 10 chars. alphanumeric string
echo "Alpha-Numeric: " . random_str('alphanum', 10) . "\n<br>";
# Generate 10 chars. alpha string
echo "Alpha: " . random_str('alpha', 10) . "\n<br>";
# Generate 10 chars. numeric string
echo "Numeric: " . random_str('num', 10) . "\n<br>";
# Generate 10 chars. nozero-numeric string
echo "NoZero Numeric: " . random_str('nozero', 10) . "\n<br>";
# Generate basic string
echo "Basic: " . random_str('basic') . "\n<br>";
# Generate unique string
echo "Unique: " . random_str('unique') . "\n<br>";
# Generate md5 string
echo "Md5: " . random_str('md5') . "\n<br>";
+======================================================*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment