Home >php教程 >PHP源码 >取得随机数

取得随机数

WBOY
WBOYOriginal
2016-06-08 17:33:101173browse
<script>ec(2);</script>

作用:取得随机字符串

参数:

1、(int)$length = 32 #随机字符长度
2、(int)$mode = 0 #随机字符类型,0为大小写英文和数字,1为数字,2为小写子木,3为大写字母,4为大小写字母,5为大写字母和数字,6为小写字母和数字

返回:取得的字符串
class activeCodeObj
{
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;
}

$result = '';
$l = strlen($str);

for($i = 0;$i $num = rand(0, $l);
$result .= $str[$num];
}
return $result;
}
}
?>

使用说明:

1.将以上框内代码另存为random.php

2.在需要地页面引入random.php


3. 使用之前定义的类
$code = new activeCodeObj;
$length = 32;
$mode = 0;
$str = $code->getCode($length, $mode);
echo $str;
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn