我可以请求一段生成包含随机字母和数字的单词的代码吗?我知道有一个PHP函数rand(),但我不知道它是否适用于字母。还有一个名为mt_rand()的函数,但我不知道它是如何工作的。我打算生成像这样的一个单词:
$randomcode = re784dfg7ert7;
你们有没有关于这个问题的简单代码?提前谢谢!
我可以请求一段生成包含随机字母和数字的单词的代码吗?我知道有一个PHP函数rand(),但我不知道它是否适用于字母。还有一个名为mt_rand()的函数,但我不知道它是如何工作的。我打算生成像这样的一个单词:
$randomcode = re784dfg7ert7;
你们有没有关于这个问题的简单代码?提前谢谢!
// A string with random letters and numbers. A-Z, a-z, 0-9
// A function in PHP is a block of code that can be used elsewhere in code.
// This function is called rand_string and will generate a random sequence of characters in one string.
// A default value of 16 characters is set, so that if no integer is supplied it will use the value of 16.
function rand_string($length = 16) {
// A string is something that holds alphanumeric characters and other symbols.
// This string is an empty one, or at least that's how it starts.
$string = '';
// This is known as a for/next loop, it's will run a section of code for a set number of times.
// A counter $i is incremented on each pass. In this case until it has operated $length number of times.
for ($i = 0; $i < $length; $i++) {
// This variable ($die) is assigned a random number - which is obtained via the PHP function mt_rand.
//Consult the PHP docs for more information.
$die = mt_rand(1, 3);
// This switch statement picks a case that is true and runs the accompanying code as defined in each case.
switch ($die) {
// This case will be activated if the variable $die has the value of 1. And case 2 if it has the value of 2 and so on.
case 1:
// Here and subsequently a random value between 48 and 57 is assigned to the $rnd variable.
$rnd = mt_rand(48, 57);
break;
case 2:
$rnd = mt_rand(65, 90);
break;
case 3:
$rnd = mt_rand(97, 122);
break;
}
// This is another variable $string which is assigned the ASCII character that is represented by the $rnd variable.
// ASCII characters are codes that computers use to represent characters and symbols.
// The chr function is a special PHP function that returns the character represented by the ASCII code.
// In this case the value of $rnd.
$string .= chr($rnd);
}
// Here we reach the final result. The value of $string is returned to source of the function call.
return $string;
}
// Segments of the function, loops and switches are enclosed between curly brackets {}. This limits the scope of the processing contained within.
// Usage of this function to obtain a 10 character random string.
// echo is a function that prints the result to the browser/screen.
$mystring = rand_string(10);
echo $mystring;
最简单的方法是完全掌控:
<?php
$str = 'QaR0SbT1UcV2WdX3YeZ4f5g6h7i8j9kAlBmCnDoEpFqGrHsItJuKvLwMxNyOzP';
// a-z 0-9 A-Z in above string
$shuffled = str_shuffle($str);
$shuffled = substr($shuffled,1,30);
echo $shuffled;
?>
根据您的需求更改 $str。我使用了小写字母 a 到 z,大写字母 A 到 Z 和数字值 0 到 9。
我编写了一个用于生成随机数和字符串的PHP类PHPRandomValue。
它使用“mcrypt_create_iv(4, MCRYPT_DEV_URANDOM)”生成随机数和值。我是在开发加密项目时制作的,因为我需要一个安全的随机值生成器,而mt_rand()无法满足此要求。以下是一个示例用法:
$randomValue = new RandomValue;
$randomValue->randomNumber(): = -3880998
$randomValue->randomNumberBetween(1,10): = 2
$randomValue->randomTextString(): = CfCkKDHRgUULdGWcSqP4
$randomValue->randomTextString(10): = LorPIxaeEY
$randomValue->randomKey(): = C7al8tX9.gqYLf2ImVt/!$NOY79T5sNCT/6Q.$!.6Gf/Q5zpa3
$randomValue->randomKey(10): = RDV.dc6Ai/
这将基于变量字符创建随机的字母数字字符串:
function createRandomCode()
{
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double) microtime() * 1000000);
$i = 0;
$pass = '';
while ($i <= 7)
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$random = createRandomCode();
Random::alphanumericLowercaseString($length)
,甚至可以放弃“小写字母”的要求。 - caw