PHP生成指定隨機(jī)字符串的簡(jiǎn)單實(shí)現(xiàn)方法
本文實(shí)例講述了PHP生成指定隨機(jī)字符串的簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
這是一個(gè)簡(jiǎn)單的函數(shù),沒有對(duì)生成的內(nèi)容作強(qiáng)制設(shè)定。所以在生成的字符串長(zhǎng)度較少的時(shí)候,會(huì)出現(xiàn)沒有指定類型字符的情況。當(dāng)然,修改起來也很簡(jiǎn)單,這里就不做添加了。
/**
* @param string $type
* @param $length
* @return string
*/
function randomString($type="number,upper,lower",$length){
$valid_type = array('number','upper','lower');
$case = explode(",",$type);
$count = count($case);
//根據(jù)交集判斷參數(shù)是否合法
if($count !== count(array_intersect($case,$valid_type))){
return false;
}
$lower = "abcdefghijklmnopqrstuvwxyz";
$upper = strtoupper($lower);
$number = "0123456789";
$str_list = "";
for($i=0;$i<$count;++$i){
$str_list .= $$case[$i];
}
return substr(str_shuffle($str_list),0,$length);
}
echo randomString("number,upper,lower",12);
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
Could not load type System.ServiceModel.Activation.HttpModul
本文章來詳細(xì)介紹關(guān)于Could not load type System.ServiceModel.Activation.HttpModule from assembly System.ServiceModel解決辦法,有需要的朋友可參考2012-12-12
PHP連接MySql數(shù)據(jù)庫方法簡(jiǎn)化版
MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL?AB?公司開發(fā),屬于?Oracle?旗下產(chǎn)品。MySQL?是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,這篇文章主要介紹了PHP連接mysql數(shù)據(jù)庫,數(shù)據(jù)庫連接靜態(tài)工具類,簡(jiǎn)化連接2022-07-07
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)組合模式與裝飾模式詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)組合模式與裝飾模式,結(jié)合實(shí)例形式詳細(xì)分析了php組合模式與裝飾模式的定義、功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12
PHP圖片處理之圖片旋轉(zhuǎn)和圖片翻轉(zhuǎn)實(shí)例
這篇文章主要介紹了PHP圖片處理之圖片旋轉(zhuǎn)和圖片翻轉(zhuǎn)實(shí)例,本文使用imagerotate函數(shù)實(shí)現(xiàn),自定義了多個(gè)函數(shù)來實(shí)現(xiàn)功能需求,需要的朋友可以參考下2014-11-11

