当前位置:首页 > PHP教程 > PHP高级教程

PHP获取redis里不存在的6位随机数应用示例【设置24小时过时】

本文实例讲述了php获取redis里不存在的6位随机数的方法。分享给大家供大家参考,具体如下:

php获取6位数随机数

php str_shuffle() 函数

str_shuffle() 函数随机打乱字符串中的所有字符。

参数 描述
string 必需。规定要打乱的字符串。

用php的str_shuffle函数:

<?php $randstr = str_shuffle('abcdefghijklmnopqrstuvwxyz1234567890'); $rand = substr($randstr,0,6); ?>

实例:获取redis里面不存在的6位随机数(设置24小时过时)

$port_number = '1605d1bcc6c8027ba0223147652d67d6'; $send_number = $this->getsixrandnumber(); $rs = $this->redis->setkeyvalue('ports:' . $send_number,$port_number); //以秒为最小单位 $this->redis->settimeout('ports:' . $send_number,24*3600); /** * 获取6位数随机数 */ protected function getsixrandnumber(){ $randstr = str_shuffle('1234567890'); $rand = substr($randstr,0,6); $port = $this->redis->getitembykey('ports:' .$rand); //存在的重新取 if($port != null){ return $this->getsixrandnumber(); } return $rand; }

ps:这里再为大家提供两款功能类似的在线工具供大家参考:

在线随机数字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu

高强度密码生成器:
http://tools.jb51.net/password/createstrongpassword

更多关于php相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数学运算技巧总结》、《php数组(array)操作技巧大全》及《php常见数据库操作技巧汇总》

希望本文所述对大家php程序设计有所帮助。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!