<?php
namespace leyangjun\Lib;
class RandString
{
// private static $lowerLetters = 'abcdefghijklmnopqrstuvwxyz'; //小写字母
// private static $upperLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //大写字母
// private static $numeric = '0123456789'; //数字
private static $string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; //数字+大小写字母
private static $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; //大小写字母
private static $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()'; //数字+大小写字母+特殊字符
private static function generate($minLength, $maxLength, $seedString)
{
if ($minLength <= 0) {
return '';
}
if ($minLength > $maxLength) {
$len = $minLength;
} else {
$len = mt_rand($minLength, $maxLength);
}
$strCnt = strlen($seedString);
$strMaxIdx = $strCnt - 1;
$result = '';
for ($i = 0; $i < $len; $i++) {
$idx = mt_rand(0, $strMaxIdx);
$result .= $seedString[$idx];
}
return $result;
}
public static function string($minLength, $maxLength = 0)
{
return self::generate($minLength, $maxLength, self::$string);
}
public static function letters($minLength, $maxLength = 0)
{
return self::generate($minLength, $maxLength, self::$letters);
}
public static function chars($minLength, $maxLength = 0)
{
return self::generate($minLength, $maxLength, self::$chars);
}
}
PHP生成随机字符串类封装
最新推荐文章于 2021-03-20 05:16:25 发布
本文介绍了一个用PHP编写的类,用于生成指定长度的随机字符串。该类支持生成包含数字、字母及特殊字符的字符串,提供了灵活性高的字符串生成选项。
313





