PHP Random String Function


/ Published in: PHP
Save to your folder(s)

Generates a random string of a given length and character set.


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * Generates a random string of a given length and character set.
  4.  * @param int $l The length of the resulting string
  5.  * @param string $c The character set
  6.  * @return string The random sequence
  7.  */
  8. function mt_rand_str ($l, $c = 'abcdefghijklmnopqrstuvwxyz1234567890') {
  9. for ($s = '', $cl = mb_strlen($c)-1, $i = 0; $i < $l; $s .= mb_substr($c, mt_rand(0, $cl), 1), ++$i);
  10. return $s;
  11. }
  12. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.