Last active
January 24, 2018 17:25
-
-
Save egulhan/95ad443f5ebf92afb9190c712f40fba8 to your computer and use it in GitHub Desktop.
Randon string generator by PHP
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 | |
/** | |
* Generates random string | |
* @param int $length | |
* @param string $charSet | |
* @return string | |
*/ | |
function generateRandomString($length = 10, $charSet = '') | |
{ | |
$str = ''; | |
if (empty($charSet)) { | |
// A-Z0-9 | |
$charSet = implode('', array_merge(range('A', 'Z'), range(0, 9))); | |
} | |
$charSetLen = strlen($charSet); | |
for ($i = 1; $i <= $length; $i++) { | |
$rndCharIndex = rand(0, $charSetLen - 1); | |
$str .= substr($charSet, $rndCharIndex, 1); | |
} | |
return $str; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment