Skip to content

Instantly share code, notes, and snippets.

@twisty
Last active December 14, 2015 05:48
Show Gist options
  • Save twisty/5037380 to your computer and use it in GitHub Desktop.
Save twisty/5037380 to your computer and use it in GitHub Desktop.
Generate an unambiguous random string from a set of possible 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