note 90773 added to function.rand
| From: | kyledotflorence | Date: | Fri, 08 May 2009 22:23:09 +0000 |
| Subject: | note 90773 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154378@lists.php.net to get a copy of this message | ||
Improved random string generation function:
<?php
// Generate a random character string
function rand_str($length = 32, $chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
// Length of character list
$chars_length = (strlen($chars) - 1);
// Start our string
$string = $chars{rand(0, $chars_length)};
// Generate random string
for ($i = 1; $i < $length; $i = strlen($string))
{
// Grab a random character from our list
$r = $chars{rand(0, $chars_length)};
// Make sure the same two characters don't appear next to each other
if ($r != $string{$i - 1}) $string .= $r;
}
// Return the string
return $string;
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 68.107.123.45
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/90773
Del: integrated -- https://master.php.net/note/delete/90773/integrated
Del: useless -- https://master.php.net/note/delete/90773/useless
Del: bad code -- https://master.php.net/note/delete/90773/bad+code
Del: spam -- https://master.php.net/note/delete/90773/spam
Del: non-english -- https://master.php.net/note/delete/90773/non-english
Del: in docs -- https://master.php.net/note/delete/90773/in+docs
Del: other reasons-- https://master.php.net/note/delete/90773
Reject -- https://master.php.net/note/reject/90773
Search -- https://master.php.net/manage/user-notes.php