Last active
December 14, 2015 05:48
-
-
Save twisty/5037380 to your computer and use it in GitHub Desktop.
Generate an unambiguous random string from a set of possible characters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate an unambiguous random string from a set of possible characters. | |
* | |
* The range of possible characters is taken from [a-z] and [0-9] with | |
* some removed to remove ambiguity if read aloud. | |
* | |
* Removed as they’re visually similar: | |
* | |
* l,1 | |
* o,0 | |
* s,5 | |
* u,v | |
* z,2 | |
* | |
* Removed as they’re phoentically similar: | |
* | |
* m,n | |
* f,s | |
*/ | |
function randomString($length = 8) | |
{ | |
$possibleChars = 'abcdeghijkpqrtwxy346789'; | |
$token = ''; | |
while(strlen($token) < $length) { | |
$token .= $possibleChars[rand(0, strlen($possibleChars) -1)]; | |
} | |
return $token; | |
} | |
echo randomString(3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment