I want to generate a code randomly with php. The length should be 5 alphanumeric chars.
But I want the first value to be A-Z. How can I do that?
I've done the following, but this seemed too obvious to me... is there another way?
function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
$str = '';
$count = strlen($charset);
while ($length--) {
$str .= $charset[mt_rand(0, $count-1)];
}
return $str;
}
$code = randString(1,'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$code .= randString(4);