note 76658 deleted from function.mt-rand by cmb
| From: | cmb@php.net | Date: | Sun, 31 Mar 2019 08:34:18 +0000 |
| Subject: | note 76658 deleted from function.mt-rand by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210458@lists.php.net to get a copy of this message | ||
Note Submitter: www.mrnaz.com
----
This function uses mt_rand() to create a random string of specified length from an optionally
supplied string of characters. It is good for generating passwords that only consist of alphanumeric
characters or codes that must consist of a defined character space such as for barcodes. It can also
be used to generate random hexadecimal numbers for things like encryption keys.
It is very fast, and allows for generation of strings of unlimited length.
<?php
function rand_string($len, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
$string = '';
for ($i = 0; $i < $len; $i++)
{
$pos = rand(0, strlen($chars)-1);
$string .= $chars{$pos};
}
return $string;
}
?>