note 91287 added to function.rand
| From: | grayda dot NOSPAM at DONTSPAM dot solidinc dot org | Date: | Thu, 04 Jun 2009 00:17:30 +0000 |
| Subject: | note 91287 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156339@lists.php.net to get a copy of this message | ||
Here's an even better random string generator:
<?php
function generateRandomString($length, $upper = false) {
# 33, 126 all characters
# 65, 90 uppercase
# 97, 122 lowercase
$str = ""; # Nothing yet
for($i = 0; $i <= $length; $i++) {
if($upper) { # If we want only uppercase
$str .= chr(rand(65,90));
} else {
$str .= chr(rand(97, 122));
}
}
}
?>
Of course this is really simple, but it's far better than the dozens of lines of code currently
used to create random strings
----
Server IP: 117.104.162.229
Probable Submitter: 203.206.183.72 (proxied: 203.206.183.72)
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/91287
Del: integrated -- https://master.php.net/note/delete/91287/integrated
Del: useless -- https://master.php.net/note/delete/91287/useless
Del: bad code -- https://master.php.net/note/delete/91287/bad+code
Del: spam -- https://master.php.net/note/delete/91287/spam
Del: non-english -- https://master.php.net/note/delete/91287/non-english
Del: in docs -- https://master.php.net/note/delete/91287/in+docs
Del: other reasons-- https://master.php.net/note/delete/91287
Reject -- https://master.php.net/note/reject/91287
Search -- https://master.php.net/manage/user-notes.php