function rand_string($len = 6, $type = '', $addChars = '') {
$str = '';
switch ($type) {
case 0 :
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
break;
case 1 :
$chars = str_repeat ( '0123456789', 3 );
break;
case 2 :
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
break;
case 3 :
$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
break;
default :
// 默認去掉了容易混淆的字符oOLl和數字01,要添加請使用addChars參數
$chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
break;
}
if ($len > 10) { //位數過長重復字符串一定次數
$chars = $type == 1 ? str_repeat ( $chars, $len ) : str_repeat ( $chars, 5 );
}
if ($type != 4) {
$chars = str_shuffle ( $chars );
$str = substr ( $chars, 0, $len );
} else {
// 中文隨機字
for($i = 0; $i < $len; $i ++) {
$str .= msubstr ( $chars, floor ( mt_rand ( 0, mb_strlen ( $chars, 'utf-8' ) - 1 ) ), 1 );
}
}
return $str;
}
摘自 lpdx111的專欄