Skip to content

Instantly share code, notes, and snippets.

@Andrewpk
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrewpk/d1bdbae0c7c9286e5f33 to your computer and use it in GitHub Desktop.
Save Andrewpk/d1bdbae0c7c9286e5f33 to your computer and use it in GitHub Desktop.
PHP: pseudo-random string of defined length
<?php
function randString($length, $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789')
{
$str = '';
$count = strlen($charset) - 1;
while ($length--) {
$str .= $charset[mt_rand(0, $count)];
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment