php 如何让“uniqid”只给出数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17576803/
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
How to make "uniqid" only give numbers?
提问by Chris
I have the following code which I have put together from different tutorial examples:
我从不同的教程示例中整理了以下代码:
<?php
$stamp = date("Ymdhis");
$random_id_length = 6;
$rndid = crypt(uniqid(rand(),1));
$rndid = strip_tags(stripslashes($rndid));
$rndid = str_replace(".","",$rndid);
$rndid = strrev(str_replace("/","",$rndid));
$rndid = substr($rndid,0,$random_id_length);
$orderid = "$stamp-$rndid";
$orderid = str_replace(".", "", "$orderid");
echo($orderid);
?>
FIDDLE: http://phpfiddle.org/main/code/27d-qfw
小提琴:http://phpfiddle.org/main/code/27d-qfw
I would like this to create a number; the current time, followed by a 6 digit random number.
我想用这个来创建一个数字;当前时间,后跟一个 6 位随机数。
For example: 20130710045730-954762
例如: 20130710045730-954762
However at the moment the random digits also include letters.
然而,目前随机数字还包括字母。
For example: 20130710045730-Z3sVN2
例如: 20130710045730-Z3sVN2
How can I edit the code to just include numbers? Any help is appreciated.
如何编辑代码以仅包含数字?任何帮助表示赞赏。
回答by hek2mgl
uniqid()
will already return numbers. But in their hexadecimal representation. In general you could just convert them to decimals:
uniqid()
将已经返回数字。但是在他们的十六进制表示中。一般来说,您可以将它们转换为小数:
echo hexdec(uniqid());
The value can only meaningful being observed on a 64 bit system as it is very large and beyond the limits of an 32bit signed integer (like php's one). And that's the point. uniqid()
uses such large numbers together with other techniques to ensure a high grade of uniqness. If you are using only 6 digits, you cannot grant this anymore.The risk that values will collide will be high.
该值只能在 64 位系统上观察到,因为它非常大并且超出了 32 位有符号整数的限制(如 php 的整数)。这就是重点。uniqid()
使用如此大的数字以及其他技术来确保高等级的唯一性。如果您仅使用 6 位数字,则不能再授予此权限。值发生冲突的风险会很高。
I would suggest to generate an application wide uniqness using an auto_increment value in a database or something similar to that.
我建议使用数据库中的 auto_increment 值或类似的东西来生成应用程序范围的唯一性。
回答by DACrosby
If it's a random string, use something like this:
如果是随机字符串,请使用以下内容:
$stamp = date("Ymdhis");
$random_id_length = 6;
$rndid = generateRandomString( $random_id_length );
$orderid = $stamp ."-". $rndid;
echo($orderid);
function generateRandomString($length = 10) {
$characters = '0123456789';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
// Output example: 20130710055714-462231
Example: http://codepad.org/eukiOb6S
示例:http: //codepad.org/eukiOb6S
Fn modified from https://stackoverflow.com/a/4356295/1265817
回答by r.vengadesh
Use this code.I think this will be help.
使用此代码。我认为这会有所帮助。
rand(100000,999999)
兰特(100000,999999)
Enter the two number which digit number do you want
输入你想要的两位数
回答by Jasveer Singh
Use this:
用这个:
str_pad(rand(0,'9'.round(microtime(true))),11, "0", STR_PAD_LEFT);