Generate Random String Using PHP

The PHP snippet below generates a random string from given characters [0-9][a-z][A-Z]. You can just copy and use this PHP snippet in your projects.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<?php function genRndString($length = 10, $chars = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ') { if($length > 0) { $len_chars = (strlen($chars) - 1); $the_chars = $chars{rand(0, $len_chars)}; for ($i = 1; $i < $length; $i = strlen($the_chars)) { $r = $chars{rand(0, $len_chars)}; if ($r != $the_chars{$i - 1}) $the_chars .= $r; } return $the_chars; } } ?>
Usage:
PHP
  • 1
  • 2
  • 3
<?php echo genRndString(); ?>
New question is currently disabled!