Re: Re: rand_str
| From: | Jeroen van Wolffelaar | Date: | Sun, 05 Aug 2001 20:50:31 +0000 |
| Subject: | Re: Re: rand_str | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-62184@lists.php.net to get a copy of this message | ||
> >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...
By the way, your function won't return strings of length $len... ;)
And you could say that preg_grep is quite a hack.
Anyway, this didn't convince me it's trivial... I'm sorry.
> >Better suggestions? Simple length and a list of characters... and the
> >list-of-characters way is also in addcslashes, and the trims.
Jeroen