note 94532 deleted from function.rand by danbrown
| From: | danbrown@php.net | Date: | Tue, 10 Nov 2009 15:33:27 +0000 |
| Subject: | note 94532 deleted from function.rand by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162701@lists.php.net to get a copy of this message | ||
Note Submitter: f dot schima at ccgmbh dot de
----
Another function to create random chars where you can specify to get only numeric, alphanumeric or
just alpha chars.
<?php
/**
* rand
*
* generate random strings
*
* @param string $length
* @param int $type type of output
* @return string
*/
function myrand( $length=4, $type=1) {
$ret = "";
for ( $i = 0; $i <= $length-1 ; ++$i) {
switch ( (int) $type ) {
/* numeric */
case 1:
$ret .= chr(rand(48, 57));
break;
/* alphanumeric */
case 2:
if ( (bool) rand(0, 1) ) {
$ret .= chr(rand(48, 57));
}
else {
$ret .= chr(rand(97, 122));
}
break;
/* alpha */
case 3:
$ret .= chr(rand(97, 122));
break;
}
}
return $ret;
}
?>