note 87373 added to function.rand
| From: | adam at greatbigmassive dot com | Date: | Tue, 02 Dec 2008 13:10:54 +0000 |
| Subject: | note 87373 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-147519@lists.php.net to get a copy of this message | ||
I've noticed alot of people doing long winded random string generators so I thought I'd
post my one liner to give a bit of a fresh approach.
The concept is simple. The rand() function generates a number made up of 10 different digits.
(0,1,2,3,4,5,6,7,8,9).
To generate an equivalent string I only need 10 alpha characters to replace the numbers.
I am using (p,q,r,s,t,u,v,w,x,y). I chose these letters because they don't include any
characters that resemble numbers and was the best set generated by the chr() function.
Anyway, to create a 6 character random string I do this:
$rand = preg_replace("/([0-9])/e","chr((\\1+112))",rand(100000,999999));
The length of the string is based on the length of the random number generated.
This would also mean you could generate a random string at a random length too if you wanted.
Adam
----
Server IP: 87.124.35.190
Probable Submitter: 85.158.139.99
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/87373
Del: integrated -- https://master.php.net/note/delete/87373/integrated
Del: useless -- https://master.php.net/note/delete/87373/useless
Del: bad code -- https://master.php.net/note/delete/87373/bad+code
Del: spam -- https://master.php.net/note/delete/87373/spam
Del: non-english -- https://master.php.net/note/delete/87373/non-english
Del: in docs -- https://master.php.net/note/delete/87373/in+docs
Del: other reasons-- https://master.php.net/note/delete/87373
Reject -- https://master.php.net/note/reject/87373
Search -- https://master.php.net/manage/user-notes.php