note 108100 added to function.rand
| From: | JustinRicher | Date: | Wed, 28 Mar 2012 13:43:25 +0000 |
| Subject: | note 108100 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186891@lists.php.net to get a copy of this message | ||
Since many people (myself included) come to this page looking for a way to do a random string, I
present a way that uses arrays and shuffle() instead of rand(). This also has the effect of not
repeating any characters in the value set.
$arr = str_split('ABCDEFGHIJKLMNOP'); // get all the characters into an array
shuffle($arr); // randomize the array
$arr = array_slice($arr, 0, 6); // get the first six (random) characters out
$str = implode('', $arr); // smush them back into a string
----
Server IP: 69.147.83.197
Probable Submitter: 129.83.31.3
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/108100
Del: integrated -- https://master.php.net/note/delete/108100/integrated
Del: useless -- https://master.php.net/note/delete/108100/useless
Del: bad code -- https://master.php.net/note/delete/108100/bad+code
Del: spam -- https://master.php.net/note/delete/108100/spam
Del: non-english -- https://master.php.net/note/delete/108100/non-english
Del: in docs -- https://master.php.net/note/delete/108100/in+docs
Del: other reasons-- https://master.php.net/note/delete/108100
Reject -- https://master.php.net/note/reject/108100
Search -- https://master.php.net/manage/user-notes.php