Random string
Generace náhodného řetězce
php
<?php
/** Vygeneruje náhodný řětězec v požadovaném rozsahu znaků dle zadání
* @param int
* @param string (a-z,A-Z,0-9)
* @return string
*/
function random($length = 10, $charList = 'a-z,0-9'){
$charList = explode(',', $charList);
$string = null;
foreach($charList as $list){
$chars = explode('-', $list);
foreach(range($chars[0], $chars[1]) as $allChars){
$string = $string . $allChars;
}
}
$randomString = null;
for ($i = 0; $i < $length; $i++){
$rankKey = mt_rand(0, (strlen($string) - 1));
$randomString = $randomString . $string[$rankKey];
}
return $randomString;
}
//Použití
echo random(32, 'a-z') . '<br>';
echo random(32, 'A-Z') . '<br>';
echo random(32, '0-9') . '<br>';
echo random(32) . '<br>';
echo random(32, 'A-Z,0-9') . '<br>';
echo random(32, 'a-z,A-Z,0-9') . '<br>';
Neformátovaný
Přidáno: 11.7.2015
Expirace: Neuvedeno