PHP獲取redis里不存在的6位隨機(jī)數(shù)應(yīng)用示例【設(shè)置24小時過時】
本文實例講述了PHP獲取redis里不存在的6位隨機(jī)數(shù)的方法。分享給大家供大家參考,具體如下:
PHP獲取6位數(shù)隨機(jī)數(shù)
PHP str_shuffle() 函數(shù)
str_shuffle() 函數(shù)隨機(jī)打亂字符串中的所有字符。
| 參數(shù) | 描述 |
|---|---|
| string | 必需。規(guī)定要打亂的字符串。 |
用php的str_shuffle函數(shù):
<?php
$randStr = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
$rand = substr($randStr,0,6);
?>
實例:獲取redis里面不存在的6位隨機(jī)數(shù)(設(shè)置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位數(shù)隨機(jī)數(shù)
*/
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:這里再為大家提供兩款功能類似的在線工具供大家參考:
在線隨機(jī)數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高強(qiáng)度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php做下載文件的實現(xiàn)代碼及文件名中亂碼解決方法
php做下載文件的實現(xiàn)代碼及文件名中亂碼解決方法,需要的朋友可以參考下。2011-02-02

