- floor: documentation ( source)
- ord: documentation ( source)
<?php
function password($len = 12) {
$chars = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$len = strlen($chars);
$cutoff = floor(256 / $len) * $len;
for (;;) {
$byte = ord(openssl_random_pseudo_bytes(1)[0]);
if ($byte >= $cutoff) {
continue;
}
return $byte % $len;
}
}
echo password();