Re: [PHP-DEV] Re: rand_str
i would say so, yes. and if you do md5(microtime()) you have to do a substr() on the result to get the desired length. easy done, yes. but still.. maybe something for PEAR instead of the core? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 3:41 Subject: Re: [PHP-DEV] Re: rand_str is it that much more useful than md5(microtime()) ? ah-oh, you want to be able to specify the set of characters... At 04:28 8/5/2001, Daniel Andersson wrote the following: -- sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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] --end of quote-- [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]
Re: [PHP-DEV] Re: rand_str
I think that new functions should be added on basis of usefulness, not the coolness factor. IMNSHO this function isn't very useful, and it is extremely easy to implement in userland: function str_rand() { $len = func_num_args() ? func_get_arg(0) : 32 ; return substr(md5(microtime()),0,$len); } At 13:57 8/5/2001, Daniel Andersson wrote the following: -- i would say so, yes. and if you do md5(microtime()) you have to do a substr() on the result to get the desired length. easy done, yes. but still.. maybe something for PEAR instead of the core? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 3:41 Subject: Re: [PHP-DEV] Re: rand_str is it that much more useful than md5(microtime()) ? ah-oh, you want to be able to specify the set of characters... At 04:28 8/5/2001, Daniel Andersson wrote the following: -- sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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] --end of quote-- [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] --end of quote-- [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]
Re: [PHP-DEV] Re: rand_str
yes, totally agree. but why not put it into PEAR? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 13:52 Subject: Re: [PHP-DEV] Re: rand_str I think that new functions should be added on basis of usefulness, not the coolness factor. IMNSHO this function isn't very useful, and it is extremely easy to implement in userland: function str_rand() { $len = func_num_args() ? func_get_arg(0) : 32 ; return substr(md5(microtime()),0,$len); } At 13:57 8/5/2001, Daniel Andersson wrote the following: -- i would say so, yes. and if you do md5(microtime()) you have to do a substr() on the result to get the desired length. easy done, yes. but still.. maybe something for PEAR instead of the core? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 3:41 Subject: Re: [PHP-DEV] Re: rand_str is it that much more useful than md5(microtime()) ? ah-oh, you want to be able to specify the set of characters... At 04:28 8/5/2001, Daniel Andersson wrote the following: -- sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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] --end of quote-- [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] --end of quote-- [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]
Re: [PHP-DEV] Re: rand_str
I'm not against putting it into pear. ask the pear guys what they think... but there's no Strings class AFAIK, and I think they'll hesitate to create a top-level class for just one function. :) At 16:47 8/5/2001, Daniel Andersson wrote the following: -- yes, totally agree. but why not put it into PEAR? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 13:52 Subject: Re: [PHP-DEV] Re: rand_str I think that new functions should be added on basis of usefulness, not the coolness factor. IMNSHO this function isn't very useful, and it is extremely easy to implement in userland: function str_rand() { $len = func_num_args() ? func_get_arg(0) : 32 ; return substr(md5(microtime()),0,$len); } At 13:57 8/5/2001, Daniel Andersson wrote the following: -- i would say so, yes. and if you do md5(microtime()) you have to do a substr() on the result to get the desired length. easy done, yes. but still.. maybe something for PEAR instead of the core? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 3:41 Subject: Re: [PHP-DEV] Re: rand_str is it that much more useful than md5(microtime()) ? ah-oh, you want to be able to specify the set of characters... At 04:28 8/5/2001, Daniel Andersson wrote the following: -- sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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] --end of quote-- [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] --end of quote-- [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 --end of quote-- [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]
Re: [PHP-DEV] Re: rand_str
heh.. possibly. ;) forwarded. / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 15:59 Subject: Re: [PHP-DEV] Re: rand_str I'm not against putting it into pear. ask the pear guys what they think... but there's no Strings class AFAIK, and I think they'll hesitate to create a top-level class for just one function. :) At 16:47 8/5/2001, Daniel Andersson wrote the following: -- yes, totally agree. but why not put it into PEAR? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 13:52 Subject: Re: [PHP-DEV] Re: rand_str I think that new functions should be added on basis of usefulness, not the coolness factor. IMNSHO this function isn't very useful, and it is extremely easy to implement in userland: function str_rand() { $len = func_num_args() ? func_get_arg(0) : 32 ; return substr(md5(microtime()),0,$len); } At 13:57 8/5/2001, Daniel Andersson wrote the following: -- i would say so, yes. and if you do md5(microtime()) you have to do a substr() on the result to get the desired length. easy done, yes. but still.. maybe something for PEAR instead of the core? / d - Original Message - From: Cynic [EMAIL PROTECTED] To: Daniel Andersson [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, August 05, 2001 3:41 Subject: Re: [PHP-DEV] Re: rand_str is it that much more useful than md5(microtime()) ? ah-oh, you want to be able to specify the set of characters... At 04:28 8/5/2001, Daniel Andersson wrote the following: -- sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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]
Re: [PHP-DEV] Re: rand_str
but why not put it into PEAR? PEAR can be useful, but the power of PHP is, that is has so many helpful build-in functions. And with pear, it will always be longer. I think that new functions should be added on basis of usefulness, not the coolness factor. IMNSHO this function isn't very useful, To generate random passwords, with characters you decide, and length? I think it is useful, but usefulness indeed is a good point. Does everyone here doubt the usefulness? and it is extremely easy to implement in userland: That is not true, md5 will always return hex, and is of specific length. I can image users don't want (so much) digits, and/or want to exclude certain look-alike chars ( 0 (zero) and O (o), for example). Greetz, Jeroen -- 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]
Re: [PHP-DEV] Re: rand_str
At 17:21 8/5/2001, Jeroen van Wolffelaar wrote the following: -- but why not put it into PEAR? PEAR can be useful, but the power of PHP is, that is has so many helpful build-in functions. And with pear, it will always be longer. I think that new functions should be added on basis of usefulness, not the coolness factor. IMNSHO this function isn't very useful, To generate random passwords, with characters you decide, and length? I think it is useful, but usefulness indeed is a good point. Does everyone here doubt the usefulness? and it is extremely easy to implement in userland: That is not true, So how about this? function str_rand($len=8) { $retval = strtr(md5(microtime()), chr(0x30), chr(0x4F)); return substr($retval,0,$len); } for($i=0; $i10; $i++){ echo str_rand(), \n; } md5 will always return hex, and is of specific length. I can image users don't want (so much) digits, and/or want to exclude certain look-alike chars ( 0 (zero) and O (o), for example). [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]
Re: [PHP-DEV] Re: rand_str
and it is extremely easy to implement in userland: That is not true, So how about this? function str_rand($len=8) { $retval = strtr(md5(microtime()), chr(0x30), chr(0x4F)); return substr($retval,0,$len); } for($i=0; $i10; $i++){ echo str_rand(), \n; } I find rand_str($len , 1..9a..f\x4F) easier... ;-) And this is still yielding exactly 16 differerent characters. As you saw for yourself, it is not trivial to implement something like this in userland. For example str_pad is way easier to implement, only some playing with str_repeat. Are there any objections if I implement this? Are you still against this new function? Jeroen -- 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]
Re: [PHP-DEV] Re: rand_str
At 21:17 8/5/2001, Jeroen van Wolffelaar wrote the following: -- [...] function str_rand($len=8) { $retval = strtr(md5(microtime()), chr(0x30), chr(0x4F)); return substr($retval,0,$len); } for($i=0; $i10; $i++){ echo str_rand(), \n; } I find rand_str($len , 1..9a..f\x4F) easier... ;-) And this is still yielding exactly 16 differerent characters. As you saw for yourself, it is not trivial to implement something like this in userland. For example str_pad is way easier to implement, only some playing with str_repeat. Are there any objections if I implement this? Are you still against this new function? Jeroen Hi Jeroen, I think we're not on the same page. :) I consider both versions of str_rand() I posted trivial... Also, and this is strictly subjective, I don't think the function needs to be implemented natively. As I said, it's trivial. :) But my stance could be rated as -0.3... Actually, as I'm thinking about it, -0.2. as for the rand_str($len , 1..9a..f\x4F) proto: I don't like it. KISS. [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]
Re: [PHP-DEV] Re: rand_str
Hi Jeroen, I think we're not on the same page. :) I consider both versions of str_rand() I posted trivial... Agree. But they are not what rand_str could do. The result has 16 different chars, just because md5 happens to have that much. Implementing something that has NOT that limitation, is far less trivial. Below is a - limited - implementation Also, and this is strictly subjective, I don't think the function needs to be implemented natively. As I said, it's trivial. :) But my stance could be rated as -0.3... Actually, as I'm thinking about it, -0.2. k, mine is 0.8, 0.9, on the idea, not the details as for the rand_str($len , 1..9a..f\x4F) proto: I don't like it. KISS. Better suggestions? Simple length and a list of characters... and the list-of-characters way is also in addcslashes, and the trims. Jeroen -- 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]
Re: [PHP-DEV] Re: rand_str
At 21:53 8/5/2001, Jeroen van Wolffelaar wrote the following: -- Hi Jeroen, I think we're not on the same page. :) I consider both versions of str_rand() I posted trivial... Agree. But they are not what rand_str could do. The result has 16 different chars, just because md5 happens to have that much. 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() * 100); $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? :) Also, and this is strictly subjective, I don't think the function needs to be implemented natively. As I said, it's trivial. :) But my stance could be rated as -0.3... Actually, as I'm thinking about it, -0.2. k, mine is 0.8, 0.9, on the idea, not the details as for the rand_str($len , 1..9a..f\x4F) proto: I don't like it. KISS. Better suggestions? Simple length and a list of characters... and the list-of-characters way is also in addcslashes, and the trims. Jeroen --end of quote-- [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]
Re: [PHP-DEV] Re: rand_str
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() * 100); $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 -- 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]
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() * 100); $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... Fr 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]
Re: [PHP-DEV] Re: rand_str
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() * 100); $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... Fr 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]
[PHP-DEV] Re: rand_str
sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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]
Re: [PHP-DEV] Re: rand_str
is it that much more useful than md5(microtime()) ? ah-oh, you want to be able to specify the set of characters... At 04:28 8/5/2001, Daniel Andersson wrote the following: -- sounds useful and cool, me thinks :o) / d [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, I'm thinking about a new function: [mt_]rand_str Syntax: string [mt_]rand_str(minlen,maxlen[,charlist]); where charlist is in addcslashes (and now also [l|r]trim) syntax. Charlist defaults to 0..9a..zA..Z IMHO, this would be a very useful function. Any comments? Maybe not minmaxlen, but simply a fixed len? (since randomness on the length is ambigious to implement, there are less possibilities with shorter strings, which raises some issues). As an extension, the function could also accept an array as charlist, containing small strings, to produce pronouncable strings. Jeroen -- 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] --end of quote-- [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]