PHP random string


I needed to generate random usernames for creating random users and wanted to use a random string generator.

There are quite some elaborate string generators out there, but I just wanted something simple. PHP doesn't provide this natively, for some reason, so you have to build one yourself.

Since this concerned only simple alphanumeric usernames, I went with base36 instead. Luckily PHP does supply a native simple base conversion function. So this is my solution (not exactly rocket science :p):

Code: (PHP)

for ($n=0,$m=rand(4,20),$strLogin=''; $n<$m; ++$n) {
$strLogin .= base_convert(rand(0,36), 10, 36);

Hope it helps ya :)