Last active
December 8, 2023 15:11
-
-
Save sepehr/3371339 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 | |
/** | |
* Generates human-readable string. | |
* | |
* @param string $length Desired length of random string. | |
* | |
* retuen string Random string. | |
*/ | |
function readable_random_string($length = 6) | |
{ | |
$string = ''; | |
$vowels = array("a","e","i","o","u"); | |
$consonants = array( | |
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', | |
'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' | |
); | |
$max = $length / 2; | |
for ($i = 1; $i <= $max; $i++) | |
{ | |
$string .= $consonants[rand(0,19)]; | |
$string .= $vowels[rand(0,4)]; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made an update on it. You can now also add words to it. And it adds a "-" between every word(Good to use for a pass phrase).
And I also fixed the bug where you couldn't do odd numbers. Now you can do a length of 1 or 3 or 5, etc. I tried to keep it the code as short as possible :).
Info on Param:
random_words($words, $length);
$words = Amount of words you want,
$length = Amount of letters you want.
It's by default, 1 word and 6 letters. Of course you can edit it ;)