当前位置:Linux教程 - Php - 取得随机字符串

取得随机字符串

作用:取得随机字符串
<?PHP

/*

作用:取得随机字符串

参数:

    1、(int)$length = 32 #随机字符长度,默认为32

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

返回:取得的字符串

使用:

    $code = new activeCodeObj;

    $str = $code->getCode($length, $mode);

*/



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 < $length;$i ++){

            
$num = rand(0, $l);

            
$result .= $str[$num];

        }

        return
$result;

    }

}
?>