PHP万能随机字符串生成函数,支持至少包含等形式

直接上代码

if (!function_exists('rand_string')) {
    /**
     * 生成随机字符串
     * @param int $length 字符串长度
     * @param array $type 逐个字符定义随机字符集,索引数组,索引0表示默认字符集,索引i(i>0)表示第i个字符的随机字符集,字符集由基础字符集组合,对应于数组值的某个二进制位
     * @param array $codeSet 重定义内部基础字符集
     * @return string
     */
    function rand_string(int $length = 6, array $type = [], array $codeSet = [])
    {
        $set = [
            'number' => '0123456789',
            'lower' => 'abcdefghijklmnopqrstuvwxyz',
            'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'special' => '~!@#$%^&*()_+{}:\"|<>?`-=[];\'\\,./',
            'hex' => '0123456789abcdef',
            'other' => '',
        ];
        $set = array_merge($set, $codeSet);
        $combinedSet = [];
        foreach ($type as $k => $t) {
            $t = (int) $t;
            $combinedSet[$k] = '';
            if (($t & 1) > 0) {
                $combinedSet[$k] .= $set['number'];
            }
            if (($t & 2) > 0) {
                $combinedSet[$k] .= $set['lower'];
            }
            if (($t & 4) > 0) {
                $combinedSet[$k] .= $set['upper'];
            }
            if (($t & 8) > 0) {
                $combinedSet[$k] .= $set['special'];
            }
            if (($t & 16) > 0) {
                $combinedSet[$k] .= $set['hex'];
            }
            if (($t & 32) > 0) {
                $combinedSet[$k] .= $set['other'];
            }
        }
        if (!isset($combinedSet[0]) || $combinedSet[0] == '') {
            $combinedSet[0] = '0123456789';
        }
        $result = '';
        for ($i = 1; $i <= $length; $i++) {
            $charSet = (!isset($combinedSet[$i]) || $combinedSet[$i] == '') ? $combinedSet[0] : $combinedSet[$i];
            $result .= $charSet[mt_rand(0, strlen($charSet) - 1)];
        }
        return $result;
    }
}

if (!function_exists('rand_include')) {
    /**
     * 必须包含类型的随机
     * @return array
     */
    function rand_include(array $flag, int $len, int $default = null)
    {
        $flagCount = count($flag);
        if ($len < $flagCount) {
            $len = $flagCount;
        }
        $temp = range(0, $len - 1);
        $holdBits = 0;
        $result = empty($default) ? array_fill(0, $len, 0) : array_fill(0, $len + 1, $default);
        foreach ($flag as $i => $f) {
            if (is_string($f)) {
                [$f, $bits] = explode('|', $f);
                $bits = (int) $bits;
            } else {
                //最大可用位数 = 字符串长度 - 最少预留位数 - 已使用位数
                $maxBits = $len - ($flagCount - $i - 1) - $holdBits;
                $bits = mt_rand(1, $maxBits);
            }
            $holdBits += $bits;
            for ($j = 0; $j < $bits; $j++) {
                $place = mt_rand(0, count($temp) - 1);
                $result[$temp[$place] + (empty($default) ? 0 : 1)] = (int) $f;
                unset($temp[$place]);
                $temp = array_values($temp);
            }
        }
        return $result;
    }
}

//生成示例

$len = 8;

//随机数字

$str = rand_string($len);

//随机16进制(小写)

$str = rand_string($len, [16]);

//随机大写、小写、数字、特殊字符

$str = rand_string($len, [15]);

//大写字母开头,中间小写字母,数字结尾

$str = rand_string($len, [0 => 2, 1 => 4, 8 => 1]);

//大写16进制

$str = rand_string($len, [16], ['hex' => '0123456789ABCDEF']);

//字母、数字、下划线

$str = rand_string($len, [(1|2|4|32)], ['other' => '_']);

//字母、数字,且始终包含一位下划线

$str = rand_string($len, rand_include(["8|1"], $len, 7), ['special' => '_']);

//字母、数字,至少包含一位下划线

$str = rand_string($len, rand_include([8], $len, 7), ['special' => '_']);

//大写字母开头,至少包含数字、小写字母、大写字母、以及一位特殊字符

$type = rand_include([1, 2, 4, "8|1"], $len - 1);

array_unshift($type, (1|2|4|32), 4);

$str = rand_string($len, $type);

您的【点赞】与【收藏】是博主最大的创作动力!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值