如何使用 PHP 创建随机字符串?

How to create a random string using PHP?(如何使用 PHP 创建随机字符串?)
本文介绍了如何使用 PHP 创建随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我知道 PHP 中的 rand 函数会生成随机整数,但是生成随机字符串的最佳方法是什么,例如:

I know that the rand function in PHP generates random integers, but what is the best way to generate a random string such as:

原始字符串,9 个字符

$string = 'abcdefghi';

示例随机字符串限制为 6 个字符

$string = 'ibfeca';

更新:我发现了大量这些类型的函数,基本上我试图理解每个步骤背后的逻辑.

UPDATE: I have found tons of these types of functions, basically I'm trying to understand the logic behind each step.

更新:该函数应根据需要生成任意数量的字符.

UPDATE: The function should generate any amount of chars as required.

如果您回复,请评论部分.

Please comment the parts if you reply.

推荐答案

好吧,你没有澄清我在评论中提出的所有问题,但我假设你想要一个可以接受一串可能的"字符和要返回的字符串长度.为清楚起见,按照要求进行了彻底评论,使用了比我平时更多的变量:

Well, you didn't clarify all the questions I asked in my comment, but I'll assume that you want a function that can take a string of "possible" characters and a length of string to return. Commented thoroughly as requested, using more variables than I would normally, for clarity:

function get_random_string($valid_chars, $length)
{
    // start with an empty random string
    $random_string = "";

    // count the number of chars in the valid chars string so we know how many choices we have
    $num_valid_chars = strlen($valid_chars);

    // repeat the steps until we've created a string of the right length
    for ($i = 0; $i < $length; $i++)
    {
        // pick a random number from 1 up to the number of valid chars
        $random_pick = mt_rand(1, $num_valid_chars);

        // take the random character out of the string of valid chars
        // subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1
        $random_char = $valid_chars[$random_pick-1];

        // add the randomly-chosen char onto the end of our string so far
        $random_string .= $random_char;
    }

    // return our finished random string
    return $random_string;
}

要使用您的示例数据调用此函数,您可以这样称呼它:

To call this function with your example data, you'd call it something like:

$original_string = 'abcdefghi';
$random_string = get_random_string($original_string, 6);

请注意,此函数不会检查传递给它的有效字符的唯一性.例如,如果您使用 'AAAB' 的有效字符字符串调用它,则为每个字母选择 A 作为 B 的可能性要高出三倍.这可能被认为是错误或功能,取决于您的需求.

Note that this function doesn't check for uniqueness in the valid chars passed to it. For example, if you called it with a valid chars string of 'AAAB', it would be three times more likely to choose an A for each letter as a B. That could be considered a bug or a feature, depending on your needs.

这篇关于如何使用 PHP 创建随机字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)