This sort of function should be implemented in PHP. It doesn't say good
things about a language when such trivial functionality needs to be
implemented non-natively. And it doesn't need to be:

function str_rand($len = 8, $class = 'a..zA..Z0..9')
{
    if (!preg_match_all('/(.)\.\.(.)/', $class, $matches)) {
        return '';
    }
    $chars = array();
    $m1 = $matches[1];
    $m2 = $matches[2];
    for ($i = 0; $i < count($m1); $i++) {
        $chars = array_merge($chars,
            range(ord($m1[$i]), ord($m2[$i])));
    }
    if (($nchars=count($chars)) == 0) {
        return '';
    }
    else if ($nchars == 1) {
        return str_pad('', $len, chr($chars[0]));
    }
    $str = '';
    for ($i = 0; $i < $len; $i++) {
        $str .= chr($chars[mt_rand(0, $nchars-1]);
    }
    return $str;
}

A translation into C might run about 100 times faster. In most applications,
that doesn't justify sacrificing the flexibility and safety of a native
implementation.

On a related note, I don't see why PHP shouldn't ship with a standard
library written in PHP.

-Dan

----- Original Message -----
From: "Cynic" <[EMAIL PROTECTED]>
To: "Jeroen van Wolffelaar" <[EMAIL PROTECTED]>
Cc: "PHP Developers Mailing List" <[EMAIL PROTECTED]>
Sent: Sunday, August 05, 2001 5:16 PM
Subject: Re: [PHP-DEV] Re: rand_str


> At 22:50 8/5/2001, Jeroen van Wolffelaar wrote the following:
> --------------------------------------------------------------
> >> >Implementing something that has NOT that limitation, is far less
trivial.
> >>
> >> function str_rand($len = 8, $class = 'a-zA-Z1-9')
> >> {
> >>     static $init = 1;
> >>     if(1 == $init){
> >>         mt_srand((double) microtime() * 1000000);
> >>         $init = 0;
> >>     }
> >>     $chars = array();
> >>     for($i = 0; $i < $len; $i++){
> >>         $chars[] = chr(mt_rand(0,255));
> >>     }
> >>     return implode('', preg_grep('|['.preg_quote($class).']|',$chars));
> >> }
> >>
> >> Just a little bit different syntax for the second argument.
> >> What's the big deal? :)
> >
> >Okay, but hey, you're a PHP-expert with probably many years of
experience...
>
> Faaaar from that. :)
>
> >By the way, your function won't return strings of length $len... ;)
>
> Oh, shit. Optimization instead of a function. :)
> I've been sitting before the CRT for 30 hours...
>
> >And you could say that preg_grep is quite a hack.
>
> Well, it nicely fits the bill - if you use the second argument as
> a character class, preg_quote/grep() lends itself. Or do you mean the
> concatenation? That's just a matter of formatting. Take it out of the
> preg_grep() call, and it'll look better.
>
> >Anyway, this didn't convince me it's trivial... I'm sorry.
>
> ok. :) seems like the only function that'll be missing from PHP
> will be write_the_app(); :)))
>
> But this is really a non-issue. I'm fine with or without str_rand(),
> I was just trying to show that it's pretty easy to write one in PHP,
> and so the function isn't really necessary. I know, there's nl2br()
> which _is_ simple, but that's backed by the fact that in a web-oriented
> language, _everybody_ would write their own nl2br(). This is not the
> case with str_rand()...
>
> And now help me down off that soap box. :)
>
>
>
>
> [EMAIL PROTECTED]
> -------------
> And the eyes of them both were opened and they saw that their files
> were world readable and writable, so they chmoded 600 their files.
>     - Book of Installation chapt 3 sec 7
>
>
> --
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>



-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to