dongyanling9248 2013-07-10 17:10
浏览 186
已采纳

如何制作“uniqid”只给出数字?

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

I would like this to create a number; the current time, followed by a 6 digit random number.

For example: 20130710045730-954762

However at the moment the random digits also include letters.

For example: 20130710045730-Z3sVN2

How can I edit the code to just include numbers? Any help is appreciated.

  • 写回答

4条回答 默认 最新

  • doushi9474 2013-07-10 17:17
    关注

    uniqid() will already return numbers. But in their hexadecimal representation. In general you could just convert them to decimals:

    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.

    I would suggest to generate an application wide uniqness using an auto_increment value in a database or something similar to that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源