Skip to content

Instantly share code, notes, and snippets.

@petk
Last active August 29, 2015 14:06
Show Gist options
  • Save petk/7d41bcd6927f23746e20 to your computer and use it in GitHub Desktop.
Save petk/7d41bcd6927f23746e20 to your computer and use it in GitHub Desktop.
PHP random word function
<?php
function generateRandomWord($length=10, $level=2){
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$validchars[1] = "0123456789abcdfghjkmnpqrstvwxyz";
$validchars[2] = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$validchars[3] = "0123456789_!@#$%&*()-=+/abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%&*()-=+/";
$word = "";
$counter = 0;
while ($counter < $length) {
$actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
// All character must be different
if (!strstr($word, $actChar)) {
$word .= $actChar;
$counter++;
}
}
return $word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment