I'm trying to make a function that generates a random string, but the string is sometimes the wrong length.
function getRandomName($maxChars)
{
$nameChars = str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
$numOfChars = count($nameChars); // amount of characters in $nameChars
$randomString = "";
for($count=0;$count<$maxChars;$count++)
{
$randomInt = round(rand(0,$numOfChars));
$selectedChar = $nameChars[$randomInt]; // random character in $nameChars
$randomString .= $selectedChar; // adds to string
}
echo " ".strlen($randomString).", "; // length, string is echoed later
return $randomString;
}
exit(getRandomName(6)); // 6 characters long
?>
Most of the time, the result will be something like 6, s2jd8q
. But sometimes, the result is something like 5, d82kw
. I only want strings that are 6 characters long, not 5.
Anyone know how to fix this?