Re: Re: rand_str

From: Date: Sun, 05 Aug 2001 21:16:39 +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-62185@lists.php.net to get a copy of this message
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

« previous php.dev (#62185) next »