Last active
August 29, 2015 14:09
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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