作用:取得随机字符串
参数:
1、(int)$length = 32 #随机字符长度
2、(int)$mode = 0 #随机字符类型,0为大小写英文和数字,1为数字,2为小写字母,3为大写字母,4为大小写字母,5为大写字母和数字,6为小写字母和数字
定义类代码
< ?php
class getRandstrClass
{
function getCode ($length = 32, $mode = 0)
{
switch ($mode) {
case '1':
$str = '1234567890';
break;
case '2':
$str = 'abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
break;
case '6':
$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
break;
}
使用说明:
1.将以上代码另存为random.php
2.在需要的页面引入random.php
< ?php require_once('random.php'); ?>
3. 使用之前定义的类
$randString = ”;
$len = strlen($str)-1;
for($i = 0;$i < $length;$i ++){
$num = mt_rand(0, $len);
$randString .= $str[$num];
}
return $randString ;
}
}
?>
代码:
< ?php
$code = new getRandstrClass();
$length = 32;
$mode = 0;
$str = $code->getCode($length, $mode);
echo $str;
?>
发表回复