[TASK] Add random string method
This commit is contained in:
parent
b9542e5e1b
commit
a3cb4325bf
@ -45,4 +45,23 @@ class StringUtility
|
|||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRandomString(
|
||||||
|
int $length = 64,
|
||||||
|
string $keyspace = null
|
||||||
|
): string {
|
||||||
|
if (!$keyspace) {
|
||||||
|
$keyspace = '0123456789'
|
||||||
|
. 'abcdefghijklmnopqrstuvwxyz'
|
||||||
|
. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
}
|
||||||
|
if ($length < 1) {
|
||||||
|
throw new \RangeException("Length must be a positive integer");
|
||||||
|
}
|
||||||
|
$pieces = [];
|
||||||
|
$max = mb_strlen($keyspace, '8bit') - 1;
|
||||||
|
for ($i = 0; $i < $length; ++$i) {
|
||||||
|
$pieces []= $keyspace[random_int(0, $max)];
|
||||||
|
}
|
||||||
|
return implode('', $pieces);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user