php生成隨機(jī)數(shù)或者字符串的代碼
更新時(shí)間:2008年09月05日 11:57:02 作者:
一個(gè)最簡單的利用php生成隨機(jī)數(shù)或者隨機(jī)字符串的函數(shù).$chars變量中的字符自己修改就能達(dá)到數(shù)字或者字符串的目的
$len表示長度,代碼如下:
/**
* 產(chǎn)生隨機(jī)字符串
*
* 產(chǎn)生一個(gè)指定長度的隨機(jī)字符串,并返回給用戶
*
* @access public
* @param int $len 產(chǎn)生字符串的位數(shù)
* @return string
*/
function randstr($len=6) {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789-@#~';
// characters to build the password from
mt_srand((double)microtime()*1000000*getmypid());
// seed the random number generater (must be done)
$password='';
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}
復(fù)制代碼 代碼如下:
/**
* 產(chǎn)生隨機(jī)字符串
*
* 產(chǎn)生一個(gè)指定長度的隨機(jī)字符串,并返回給用戶
*
* @access public
* @param int $len 產(chǎn)生字符串的位數(shù)
* @return string
*/
function randstr($len=6) {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789-@#~';
// characters to build the password from
mt_srand((double)microtime()*1000000*getmypid());
// seed the random number generater (must be done)
$password='';
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}
相關(guān)文章
Yii2 中實(shí)現(xiàn)單點(diǎn)登錄的方法
這篇文章主要介紹了Yii2 中實(shí)現(xiàn)單點(diǎn)登錄的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
ThinkPHP框架結(jié)合Ajax實(shí)現(xiàn)用戶名校驗(yàn)功能示例
這篇文章主要介紹了ThinkPHP框架結(jié)合Ajax實(shí)現(xiàn)用戶名校驗(yàn)功能,涉及thinkPHP使用ajax與后臺(tái)控制交互、數(shù)據(jù)庫查詢、判定等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
php中把美國時(shí)間轉(zhuǎn)為北京時(shí)間的自定義函數(shù)分享
這篇文章主要介紹了php中把美國時(shí)間轉(zhuǎn)為北京時(shí)間的自定義函數(shù)分享,分別提供了13小時(shí)和8小時(shí)算法版,需要的朋友可以參考下2014-07-07
Zend Framework框架的registry(注冊(cè)表)使用示例
這篇文章主要介紹了Zend Framework框架的registry(注冊(cè)表)使用示例,提供對(duì)象方式使用與set、get方法使用示例,需要的朋友可以參考下2014-03-03
PHP實(shí)現(xiàn)基于狀態(tài)的責(zé)任鏈審批模式詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)基于狀態(tài)的責(zé)任鏈審批模式,結(jié)合實(shí)例形式詳細(xì)分析了責(zé)任鏈審批模式的原理及相關(guān)php實(shí)現(xiàn)流程,需要的朋友可以參考下2019-05-05

