Posted By

paulgrenwood on 09/23/09


Tagged

php random


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

huynguye
BrunoDeBarros


Generate Random String


 / Published in: PHP
 

If you don’t need human readable string, then use this function instead, which will create a random string you can use for user id’s etc.

  1. /*************
  2. *@l - length of random string
  3. */
  4. function generate_rand($l){
  5. $c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  6. srand((double)microtime()*1000000);
  7. for($i=0; $i<$l; $i++) {
  8. $rand.= $c[rand()%strlen($c)];
  9. }
  10. return $rand;
  11. }

Report this snippet  

You need to login to post a comment.