1.生成指定长度的汉字字符串

/**
 * 生成指定长度的汉字字符串
 * @param int $length 
 * @return string
 */
function random_chinese_characters(int $length = 1): string
{
    $random_chinese_characters = '';
    for ($i = 1; $i <= $length; $i++) {
        $random_chinese_characters .= mb_chr(mt_rand(0x4e00, 0x9fff), 'utf-8');
    }
    return $random_chinese_characters;
}

2.生成指定长度的随机半角字符串

/**
 * 生成指定长度的随机半角字符串
 * @param int $length
 * @return string
 */
function random_half_width_characters(int $length = 1): string
{
    $random_half_width_characters = '';
    for ($i = 1; $i <= $length; $i++) {
        $random_half_width_characters .= chr(mt_rand(0x21, 0x7E));
    }
    return $random_half_width_characters;
}

3.生成指定长度的字母数字组合字符串

/**
 * 生成指定长度的字母数字组合字符串
 * @param int $length
 * @return string
 */
function random_letter_number_characters(int $length = 1): string
{
    $random_ascii = function () {
        RANDOM_ASCII:
        $ascii = mt_rand(0x30, 0x7A);
        if ($ascii > 0x2F && $ascii < 0x3A || $ascii > 0x40 && $ascii < 0x5B || $ascii > 0x60 && $ascii < 0x7B) {
            return $ascii;
        } else {
            goto RANDOM_ASCII;
        }
    };
    $random_letter_number_characters = '';
    for ($i = 1; $i <= $length; $i++) {
        $random_letter_number_characters .= chr($random_ascii());
    }
    return $random_letter_number_characters;
}

4.生成指定长度的随机的指定字符串

(1)方法一
/**
 * 生成指定长度的随机的指定字符串
 * @param string $characters
 * @param int $length
 * @return string
 */
function random_characters(string $characters, int $length = 1): string
{
    $random_characters = '';
    $characters_length = mb_strlen($characters);
    for ($i = 1; $i <= $length; $i++) {
        $random_characters .= mb_substr($characters, mt_rand(0, $characters_length - 1), 1, 'utf-8');
    }
    return $random_characters;
}

(2)方法二(PHP 7 >= 7.4.0)
/**
 * 生成指定长度的随机的指定字符串
 * @param string $characters
 * @param int $length
 * @return string
 */
function random_characters(string $characters, int $length = 1): string
{
    $characters = mb_str_split($characters, 1, 'utf-8');
    $random_characters = '';
    $characters_length = count($characters);
    for ($i = 1; $i <= $length; $i++) {
        $random_characters .= $characters[mt_rand(0, $characters_length - 1)];
    }
    return $random_characters;
}

5.生成指定长度的韩文字符串

/**
 * 生成指定长度的韩文字符串
 * @param int $length
 * @return string
 */
function random_korean_characters(int $length = 1): string
{
    $random_korean_characters = '';
    for ($i = 1; $i <= $length; $i++) {
        $random_korean_characters .= mb_chr(mt_rand(0xac00, 0xd7af), 'utf-8');
    }
    return $random_korean_characters;
}

6.生成指定长度的日文字符串

/**
 * 生成指定长度的日文字符串
 * @param int $length
 * @return string
 */
function random_japanese_characters(int $length = 1): string
{
    $random_japanese_characters = '';
    for ($i = 1; $i <= $length; $i++) {
        $random_japanese_characters .= mb_chr(mt_rand(0x3040, 0x30FF), 'utf-8');
    }
    return $random_japanese_characters;
}

最后修改:2020 年 04 月 14 日 07 : 54 PM
如果觉得我的文章对你有用,请随意赞赏