Random String in PHP
您好,我想在PHP中生成随机字符串,重复性如ag7hla。有什么办法吗?我在试这个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?php
function random_string ($length)
{
$length = (int ) $length;
if ($length < 1) return '';
$base ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
$max = strlen($base) - 1;
$string = '';
while ($len--)
{
$string = $base[mt_rand(0, $max)];
}
return $string;
}
?> |
但是没有生成随机字符串
- 遵循堆栈溢出的链接(同样的问题):stackoverflow.com/questions/4356289/&hellip;
1 2 3 4 5 6 7 8
| function generateRandomString ($length = 10) {
$char = 'abcdefghijklmnopqrstuvwxyz';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $char[rand(0, strlen($char) - 1)];
}
return $randomString;
} |
如果你也想增加数字,那么$char将是--->
1
| $char = 'abcdefghijklmnopqrstuvwxyz1234567890'; |
如果是资本案例--->
1
| $char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; |
- 我试过了,但没用
- 我试过上面的函数,最后echo generaterandomstring();生成的字符串只是一个字母表