单行PHP随机字符串生成器? [英] One-line PHP random string generator?

查看:88
本文介绍了单行PHP随机字符串生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找生成随机/唯一字符串的最短方法,为此,我使用了以下两种方法:

I am looking for the shortest way to generate random/unique strings and for that I was using the following two:

$cClass = sha1(time());

$cClass = md5(time());

但是,我需要以字母字符开头的字符串,我正在查看base64编码,但这在末尾添加了==,然后我需要摆脱它.

However, I need the string to begin with an an alphabet character, I was looking at base64 encoding but that adds == at the end and then I would need to get rid of that.

用一行代码实现这一目标的最佳方法是什么?

What would be the best way to achieve this with one line of code?

更新:

PRNDL提出了一个很好的建议,直到我最终使用它但对其进行了一些修改

PRNDL came up with a good suggestions wich I ended up using it but a bit modified

echo substr(str_shuffle(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ),0, 1) . substr(str_shuffle(aBcEeFgHiJkLmNoPqRstUvWxYz0123456789),0, 31)

将产生32个模仿md5哈希的字符,但总是将第一个字符生成一个字母,像这样;

Would yield 32 characters mimicking the md5 hash but it would always product the first char an alphabet letter, like so;

但是,您的确实有所改善,他的回答也得到了改善

However, Uours really improved upon and his answer;

substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1).substr(md5(time()),1);

更短更甜美

Anonymous2011 的另一项建议非常棒,但由于某种原因,第一个字符始终是M,N,Y,Z,因此不符合我的目的,但会被选择为答案,顺便说一句有谁知道为什么总是会产生那些特殊的字母?

The other suggestion by Anonymous2011 was very awesome but the first character for some reason would always either M, N, Y, Z so didnt fit my purposes but would have been the chosen answer, btw does anyone know why it would always yield those particular letters?

这是我修改后的版本的预览

Here is the preview of my modified version

echo  rtrim(base64_encode(md5(microtime())),"=");

推荐答案

与其改组字母字符串,不如改用单个随机char,会更快.

从字符串中获取单个随机字符,然后将md5( time( ) )附加到字符串中.在附加md5( time( ) )之前,请从其中删除一个字符,以使结果字符串的长度保持为32个字符:

Rather than shuffling the alphabet string , it is quicker to get a single random char .

Get a single random char from the string and then append the md5( time( ) ) to it . Before appending md5( time( ) ) remove one char from it so as to keep the resulting string length to 32 chars :

substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", mt_rand(0, 51), 1).substr(md5(time()), 1);

小写版本:

substr("abcdefghijklmnopqrstuvwxyz", mt_rand(0, 25), 1).substr(md5(time()), 1);

甚至更短,更快一点的小写版本:

Or even shorter and a tiny bit faster lowercase version :

chr(mt_rand(97, 122)).substr(md5(time()), 1);

/* or */

chr(mt_rand(ord('a'), ord('z'))).substr(md5(time()), 1);


给任何试图在一秒钟内生成许多随机字符串的人的注释:
time( ) 以秒为单位返回时间, md5( time( ) ) 在给定的第二次时间内将是相同的,因此,如果在第二次之内生成了许多随机字符串,那么这些字符串可能最终会重复一些.


我已经使用以下代码进行了测试.这将测试小写版本:


I have tested using below code . This tests lower case version :

    $num_of_tests = 100000;

    $correct = $incorrect = 0;

    for( $i = 0; $i < $num_of_tests; $i++ )
    {
        $rand_str = substr( "abcdefghijklmnopqrstuvwxyz" ,mt_rand( 0 ,25 ) ,1 ) .substr( md5( time( ) ) ,1 );

        $first_char_of_rand_str = substr( $rand_str ,0 ,1 );

        if( ord( $first_char_of_rand_str ) < ord( 'a' ) or ord( $first_char_of_rand_str ) > ord( 'z' ) )
        {
            $incorrect++;
            echo $rand_str ,'<br>';
        }
        else
        {
            $correct++;
        }
    }

    echo 'Correct: ' ,$correct ,' . Incorrect: ' ,$incorrect ,' . Total: ' ,( $correct + $incorrect );

这篇关于单行PHP随机字符串生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆