本文實例講述了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;}
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選