Posted By

kentandre on 01/26/11


Tagged

random generator String generate


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

kendsnyder
jirimelcak


Random string generator


 / Published in: PHP
 

  1. function str_makerand($length, $useupper, $usespecial, $usenumbers) {
  2. $charset = "abcdefghijklmnopqrstuvwxyz";
  3. if ($useupper)
  4. $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  5. if ($usenumbers)
  6. $charset .= "0123456789";
  7. if ($usespecial)
  8. $charset .= "~@#$%^*()_+-={}|]["; // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./";
  9. if ($length == '' || $length == 0)
  10. $length = mt_rand(10, 40);
  11. for ($i = 0; $i < $length; $i++)
  12. $key .= $charset[(mt_rand(0, (strlen($charset) - 1)))];
  13. return $key;
  14. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: kendsnyder on January 26, 2011

I'd also recommend removing all the vowels so you don't have to worry about bad words in your result.

You need to login to post a comment.