Skip to content

Instantly share code, notes, and snippets.

@sepehr
Last active December 8, 2023 15:11
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save sepehr/3371339 to your computer and use it in GitHub Desktop.
Save sepehr/3371339 to your computer and use it in GitHub Desktop.
PHP: Human-readable Random String
<?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;
}
@laurent22
Copy link

Works great, thanks a lot for sharing.

@laurent22
Copy link

The only change I made is that I removed the srand call since a self-contained function shouldn't change global state. Also by default PHP's random seed is already initialised to a good enough value.

@serdarde
Copy link

serdarde commented Oct 6, 2018

Nice function, thanks

@Seunope
Copy link

Seunope commented Feb 23, 2019

Nice! Works like charm.

@nesrindagli
Copy link

Works NICE, thanks!

@rjworks
Copy link

rjworks commented Jul 7, 2020

thanks my brother

@indextwo
Copy link

This is great, but I'd definitely remove the srand() function. Called in a loop 100,000 times, with a character length of 8, without that srand(), there are on average ~50 collisions (i.e. 50 non-unique words generated), which is 0.05%. Adding it back in increases the number of collisions a hundred-fold - anywhere from 4800 to 5600 collisions per loop (or ~5%).

Without srand() it's still not perfectly unique, but it is a lot better.

@INirandjan
Copy link

INirandjan commented Mar 15, 2021

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 ;)

function random_words($words = 1, $length = 6)
{  
    $string = '';
    for ($o=1; $o <= $words; $o++) 
    { 
        $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'
        );  
    
        $word = '';
        for ($i = 1; $i <= $length; $i++)
        {
            $word .= $consonants[rand(0,19)];
            $word .= $vowels[rand(0,4)];
        }
        $string .= mb_substr($word, 0, $length);
        $string .= "-";
    }
    return mb_substr($string, 0, -1);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment