note 74296 added to function.rand

From: Date: Tue, 03 Apr 2007 22:12:44 +0000
Subject: note 74296 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-125070@lists.php.net to get a copy of this message
A lot of developers need to generate random id depending on any format defined by e.g. any config switch. Here is example of function, which will generate random id depending on specified format /** * Generates random string depending on format * Z - represents any character between A-Z * X - represents characters 0-9, a-z, A-Z * z - represents any character between a-z * 9 - represents any number between 0-9 * all other characters are used as in format */ function getRandomString($format='ZZZZZZ-99999') { $chars_Z = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); $chars_z = str_shuffle('abcdefghijklmnopqrstuvwzxy'); $chars_9 = str_shuffle('1234567890'); $chars_X = $chars_Z . $chars_z . $chars_9; $result = ''; for ($i = 0; $i < strlen($format); $i++) { switch($format[$i]) { case 'X': $result .= $chars_X[rand(0,strlen($chars_X)-1)]; break; case 'Z': $result .= $chars_Z[rand(0,strlen($chars_Z)-1)]; break; case 'z': $result .= $chars_z[rand(0,strlen($chars_z)-1)]; break; case '9': $result .= $chars_9[rand(0,strlen($chars_9)-1)]; break; default: $result .= $format[$i]; break; } } return $result; } We used this function e.g. in helpdesk software, which we develop (www.qualityunit.com/supportcenter/) for generating of ticket ids ---- Server IP: 62.65.181.66 Probable Submitter: 195.91.54.87 ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- https://master.php.net/note/edit/74296 Del: integrated -- https://master.php.net/note/delete/74296/integrated Del: useless -- https://master.php.net/note/delete/74296/useless Del: bad code -- https://master.php.net/note/delete/74296/bad+code Del: spam -- https://master.php.net/note/delete/74296/spam Del: non-english -- https://master.php.net/note/delete/74296/non-english Del: in docs -- https://master.php.net/note/delete/74296/in+docs Del: other reasons-- https://master.php.net/note/delete/74296 Reject -- https://master.php.net/note/reject/74296 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125070) next »