note 94532 added to function.rand
| From: | f dot schima at ccgmbh dot de | Date: | Tue, 10 Nov 2009 13:17:38 +0000 |
| Subject: | note 94532 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162699@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 217.160.72.57
Probable Submitter: 212.122.55.17
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/94532
Del: integrated -- https://master.php.net/note/delete/94532/integrated
Del: useless -- https://master.php.net/note/delete/94532/useless
Del: bad code -- https://master.php.net/note/delete/94532/bad+code
Del: spam -- https://master.php.net/note/delete/94532/spam
Del: non-english -- https://master.php.net/note/delete/94532/non-english
Del: in docs -- https://master.php.net/note/delete/94532/in+docs
Del: other reasons-- https://master.php.net/note/delete/94532
Reject -- https://master.php.net/note/reject/94532
Search -- https://master.php.net/manage/user-notes.php