這篇文章主要介紹了PHP取得redis裡不存在的6位隨機數的方法,可設定24小時過時限制,涉及php字串及資料庫相關操作技巧,需要的朋友可以參考下
PHP取得6位數隨機數
PHP str_shuffle()
函數
str_shuffle() 函數隨機打亂字串中的所有字元。
## string
必需。規定要打亂的字串。
用php的str_shuffle函數:
#
1 2 3 4 | <?php
$randStr = str_shuffle ('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
$rand = substr ( $randStr ,0,6);
?>
|
登入後複製
##實例:取得redis裡面不存在的6位元隨機數(設定24小時過時)
#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $port_number = '1605D1BCC6C8027BA0223147652D67D6';
$send_number = $this ->getSixRandNumber();
$rs = $this ->redis->setKeyValue('ports:' . $send_number , $port_number );
$this ->redis->setTimeout('ports:' . $send_number ,24*3600);
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 ;
}
|
登入後複製
#相關推薦:
php產生隨機數字
字、字母或數字字母混合的字串
#JS產生指定範圍隨機數字
與隨機序列方法詳解
有哪些PHP產生隨機數字
方法
######################## ######
以上是PHP取得redis裡不存在的6位數隨機數的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!