Generate random string
Snippet for generating a random string.
function getRandomString($iLength = 10, $sCharacters = null)
{
if($sCharacters == null)
$aCharacters = array_merge(range('A', 'Z'), range('a', 'z'), range(0,9));
else
$aCharacters = str_split($sCharacters);
for ($sRandomString = '', $i = 0; $i < $iLength; $i++)
$sRandomString.= $aCharacters[array_rand($aCharacters)];
return $sRandomString;
}
var_dump(getRandomString()); //Y6K7UHey4l
var_dump(getRandomString(2, 'ab')); //ba
var_dump(getRandomString(8, 'ab')); //bababbaa
var_dump(getRandomString(5, 'ABCDEFGHIJKLMO1234%&$§"$%')); //BM$M$
Url: http://sklueh.de/2013/09/zufaelligen-string-mit-php-erzeugen/
Language: PHP | User: sklueh | Created: Sep 16, 2013 | Tags: random string