note 73539 deleted from function.rand by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:45:29 +0000 |
| Subject: | note 73539 deleted from function.rand by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134449@lists.php.net to get a copy of this message | ||
Note Submitter: www.mrnaz.com
----
This function uses 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;
}
?>