第一种方法用mt_rand()
- function GetRandStr($length){
- $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $len=strlen($str)-1;
- $randstr='';
- for($i=0;$i<$length;$i++){
- $num=mt_rand(0,$len);
- $randstr .= $str[$num];
- }
- return $randstr;
- }
- $number=GetRandStr(6);
- echo $number;
第二种方法(最快的)
- function make_password( $length = 8 )
- {
- // 密码字符集,可任意添加你需要的字符
- $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
- 'i', 'j', 'k', 'l','m', 'n', 'o', 'p'