<?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;
}
$randString = '';
$len = strlen($str)-1;
for($i = 0;$i < $length;$i ++){
$num = mt_rand(0, $len);
$randString .= $str[$num];
}
return $randString ;
}
}
/* 使用方法
$code = new getRandstrClass();
$length = 4;
$mode = 0;
$str = $code->getCode($length, $mode);
echo $str;
$code = NULL;
*/
?>
PHP生成随机数的类
最新推荐文章于 2022-08-26 14:12:03 发布
利用该类,可以
生成指定长度
的纯数字字符串、字符字符串(大写、小写、大小写及大小写与数字
的组合,还可以根据喜好进行扩充),代码如下: