- substr: documentation ( source)
- str_replace: documentation ( source)
- base64_encode: documentation ( source)
- str_split: documentation ( source)
- random_bytes: documentation ( source)
<?php
// https://www.strangebuzz.com/en/snippets/convert-a-string-into-an-array-of-characters
$tokenSize = 32;
try {
$random = random_bytes($tokenSize);
} catch (\Exception $e) {
throw new \RuntimeException('Unable to get random_bytes.');
}
$base = base64_encode($random);
$clean = str_replace(str_split('+/-_'), '', $base);
$cut = substr($clean, 0, $tokenSize);
echo 'Token: '.$cut."\n";
echo 'Token length: '.strlen($cut);