Skip to content

Instantly share code, notes, and snippets.

@voku
Last active August 29, 2015 14:09
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 voku/ae1fb26c6e0514be674f to your computer and use it in GitHub Desktop.
Save voku/ae1fb26c6e0514be674f to your computer and use it in GitHub Desktop.
generate random string via php - used https://packagist.org/packages/voku/portable-utf8 (UTF8::strlen)
<?php
/**
* generate random string
*
* @param string $len length of the random string
* @param string $characters characters string for the random selection
*
* @return string
*/
function randomString($len, $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
$len = (int)$len;
$charLength = UTF8::strlen($characters) - 1;
$string = '';
if ($len > 0) {
while ($len--) {
$string .= $characters[mt_rand(0, $charLength)];
}
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment