- var_dump: documentation ( source)
- mb_strlen: documentation ( source)
- random_int: documentation ( source)
<?php
function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}
var_dump(random_str(32));
var_dump(random_str(8, 'abcdefghijklmnopqrstuvwxyz'));