Last active
January 5, 2020 09:51
-
-
Save vyspiansky/156437e70f2dcfe8e899ac5f55d3720f to your computer and use it in GitHub Desktop.
PHP: human readable random string
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 | |
/** | |
* Human Readable Random String | |
* @param int length Length of random string (must be a multiple of 2). | |
* @return string Returns the random string. | |
*/ | |
function readable_random_string($length = 8) { | |
$consonants = [ | |
"b", "c", "d", "f", "g", | |
"h", "j", "k", "l", "m", | |
"n", "p", "r", "s", "t", | |
"v", "w", "x", "y", "z" | |
]; | |
$consonants_count = count($consonants); | |
$vowels = [ | |
"a", "e", "i", "o", "u" | |
]; | |
$vowels_count = count($vowels); | |
$result = ""; | |
$max = $length/2; | |
for ($i=1; $i<=$max; $i++) { | |
$result .= $consonants[random_int(0, $consonants_count-1)]; | |
$result .= $vowels[random_int(0, $vowels_count-1)]; | |
} | |
return $result; | |
} | |
echo readable_random_string(); | |
// Possible output: "hududada", "meluxoju" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment