本文實例講述了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:2655101040 給予處理,謝謝支持。