php 任意字符串_php 生成任意长度字符串的类(只含有数字 只含有字母 混合数字和字母)...
[php]代碼庫
/*
* 生成隨機字符串的類,默認只包含數字、大小寫字母
*/
class randomString {
/*
* 生成的字符串包含的字符設置
*/
const NUMERIC_ONLY = 1; //只含有數字
const LETTER_ONLY = 2; //只含有字母
const MIXED = 3; //混合數字和字母
/*
* 用戶傳入變量,分別為字符串長度;包含的字母;是否包含大寫字母
*/
protected $length, $type, $upper;
/*
* 參數初始化
* @param int,$length 字符串長度
* @param const,$type 生成字符串的類型
* @param boolean,$upper 是否含有大寫字母
*/
public function __construct($length = 16, $type = self::MIXED, $upper = true) {
$this->length = $length;
$this->type = $type;
$this->upper = $upper;
}
/*
* 對象被轉化為字符串時調用
* @return string
*/
public function __toString() {
return $this->pickUpChars();
}
/*
* 生成隨機字符串
* @global $type
* @return string,$string
*/
public function pickUpChars() {
switch ($this->type) {
case self::NUMERIC_ONLY:
$raw = '0123456789';
break;
case self::LETTER_ONLY:
$raw = 'qwertyuioplkjhgfdsazxcvbnm' .
'QWERTYUIOPLKJHGFDSAZXCVBNM';
break;
default:
$raw = 'qwertyuioplkjhgfdsazxcvbnm' .
'QWERTYUIOPLKJHGFDSAZXCVBNM' .
'0123456789';
break;
}
$string = '';
for ($index = 0; $index < $this->length; $index++)
$string .= substr($raw, mt_rand(0, strlen($raw) - 1), 1);
if (!$this->upper)
$string = strtolower($string);
return $string;
}
}
//echo new randomString(170, randomString::MIXED, TRUE).'
';
//該片段來自于http://yuncode.net
總結
以上是生活随笔為你收集整理的php 任意字符串_php 生成任意长度字符串的类(只含有数字 只含有字母 混合数字和字母)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可根据坐姿来判断疲劳驾驶!魅族公布最新汽
- 下一篇: 又砍?iPhone 14系列照相模组零部