Re: Re: rand_str
| From: | Daniel Cowgill | Date: | Sun, 05 Aug 2001 22:07:25 +0000 |
| Subject: | Re: Re: rand_str | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-62187@lists.php.net to get a copy of this message | ||
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" <cynic@mail.cz>
To: "Jeroen van Wolffelaar" <jeroen@a-eskwadraat.nl>
Cc: "PHP Developers Mailing List" <php-dev@lists.php.net>
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. :)
>
>
>
>
> cynic@mail.cz
> -------------
> 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: php-dev-unsubscribe@lists.php.net
> For additional commands, e-mail: php-dev-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>