php 使用 chr + rand 生成随机字符(AZ)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31441050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using chr + rand to generate a random character (A-Z)
提问by michelle
I'm using the following to generate a random character from A-Z, but it's occasionally generating the @ symbol. Any idea how to prevent this? Maybe the character range is incorrect?
我正在使用以下内容从 AZ 生成一个随机字符,但它偶尔会生成 @ 符号。知道如何防止这种情况吗?也许字符范围不正确?
$letter = chr(64+rand(0,26));
回答by Tech Savant
Use this it's easier.
使用这个更容易。
Upper Case
大写
$letter = chr(rand(65,90));
Lowercase
小写
$letter = chr(rand(97,122));
The code below generates a random alpha-numeric string of $length. You can see the numbers there for what you need.
下面的代码生成一个 $length 的随机字母数字字符串。你可以在那里看到你需要的数字。
function izrand($length = 32) {
$random_string="";
while(strlen($random_string)<$length && $length > 0) {
$randnum = mt_rand(0,61);
$random_string .= ($randnum < 10) ?
chr($randnum+48) : ($randnum < 36 ?
chr($randnum+55) : $randnum+61);
}
return $random_string;
}
update: 12/19/2015
更新:12/19/2015
Here is an updated version of the function above, it adds the ability to generate a random numeric key OR an alpha numeric key. To generate numeric, simply add
the second paramater as true
.
这是上述函数的更新版本,它增加了生成随机数字键或字母数字键的能力。要生成数字,只需将第二个参数添加为true
.
Example Usage
示例用法
$randomNumber = izrand(32, true); // generates 32 digit number as string
$randomAlphaNumeric = izrand(); // generates 32 digit alpha numeric string
$randomNumber = izrand(32, true); // generates 32 digit number as string
$randomAlphaNumeric = izrand(); // generates 32 digit alpha numeric string
Typecast to Integer
类型转换为整数
If you want to typecast the number to integer, simply do this after you generate the number. NOTE: This will drop any leading zeros if they exist.
如果要将数字类型转换为整数,只需在生成数字后执行此操作。注意:这将删除任何存在的前导零。
$randomNumber = (int) $randomNumber;
$randomNumber = (int) $randomNumber;
izrand() v2
izrand() v2
function izrand($length = 32, $numeric = false) {
$random_string = "";
while(strlen($random_string)<$length && $length > 0) {
if($numeric === false) {
$randnum = mt_rand(0,61);
$random_string .= ($randnum < 10) ?
chr($randnum+48) : ($randnum < 36 ?
chr($randnum+55) : chr($randnum+61));
} else {
$randnum = mt_rand(0,9);
$random_string .= chr($randnum+48);
}
}
return $random_string;
}
回答by franciscod
ASCII code 64 is @
. You want to start at 65, which is A
. Also, PHP's rand
generates a number from min
to max
inclusive: you should set it to 25 so the biggest character you get is 90 (Z
).
ASCII 码 64 是@
. 您想从 65 开始,即A
. 此外,PHP 会rand
生成一个数字 from min
to max
inclusive:您应该将其设置为 25,这样您得到的最大字符是 90 ( Z
)。
$letter = chr(65 + rand(0, 25));
回答by luky
$range = range('A', 'Z');
$index = array_rand($range);
echo $range[$index];
回答by ka_lin
You could use, given you could generate from a-Z:
你可以使用,因为你可以从 aZ 生成:
$range = array_merge(range('A', 'Z'),range('a', 'z'));
$index = array_rand($range, 1);
echo $range[$index];
回答by Mohamed Sabr
The below code will generate an alphanumric string of 2 letters and 3 digits.
下面的代码将生成一个由 2 个字母和 3 个数字组成的字母数字字符串。
$string = strtoupper(chr(rand(65, 90)) . chr(rand(65, 90)) . rand(100, 999));
echo $string;