Autor Zpráva
blahapet
Profil
Ahojky, generuji náhodný 12ti místný kód skládající se z velkých písmen a číslic. Můžu mít otázku, co dělám špatně, když jednou to vygeneruje 12 znaků, ale podruhé třeba jen jedenáct, ale také deset nebo devět:

                                $a1 = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"); 
                                $a2 = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"); 
                                $a3 = array("1","2","3","4","5","6","7","8","9");
                                $a4 = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"); 
                                $a5 = array("1","2","3","4","5","6","7","8","9");
                                $a6 = array("1","2","3","4","5","6","7","8","9");
                                $a7 = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z");
                                $a8 = array("1","2","3","4","5","6","7","8","9");
                                $a9 = array("1","2","3","4","5","6","7","8","9");
                                $a10 = array("1","2","3","4","5","6","7","8","9");
                                $a11 = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z");
                                $a12 = array("1","2","3","4","5","6","7","8","9");
                                
                                $_a1 = rand(0, count($a1));
                                $_a2 = rand(0, count($a2));
                                $_a3 = rand(0, count($a3));
                                $_a4 = rand(0, count($a4));
                                $_a5 = rand(0, count($a5));
                                $_a6 = rand(0, count($a6));
                                $_a7 = rand(0, count($a7));
                                $_a8 = rand(0, count($a8));
                                $_a9 = rand(0, count($a9));
                                $_a10 = rand(0, count($a10));
                                $_a11 = rand(0, count($a11));
                                $_a12 = rand(0, count($a12));
                                
                                echo  $a1[$_a1].$a2[$_a2].$a3[$_a3].$a4[$_a4].$a5[$_a5].$a6[$_a6].$a7[$_a7].$a8[$_a8].$a9[$_a9].$a10[$_a10].$a11[$_a11].$a12[$_a12];

Děkuji za pomoc.
Kajman
Profil
rand(0,9) může vrátit 10 možností i když pole má 9 prvků a prvek $pole[9] neexistuje. Vypisujte si chyby včetně notice. Edit: Ten kód je hrůza.
Andrej.B
Profil
rychly check u AI.

$letters = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"); 
$numbers = array("1","2","3","4","5","6","7","8","9");

// Kombinácia polí, kde je daný index polí, ktorý sa má použiť.
$combination = array($letters, $letters, $numbers, $letters, $numbers, $numbers, $letters, $numbers, $numbers, $numbers, $letters, $numbers);

// Výstupný reťazec
$result = "";

foreach ($combination as $array) {
    $result .= $array[array_rand($array)];
}

echo $result;
blahapet
Profil
Andrej B. - díky moc!
N71
Profil *
Připomíná to trochu soutěž o to, jak nejvíc prasecky generovat "náhodné" řetězce, pánové. A Andrej.B si příště kódy generované jazykovými modely ponechá pro sebe a nebude je vydávat za nějakou radu.

Osobně bych volil spíš kryptograficky bezpečný zdroj náhodných dat, jako random_bytes() a pak psal něco jako:

substr(strtoupper(preg_replace('/\W/', '', base64_encode(random_bytes(30)))), 0, 12);
blahapet
Profil
N71:
Díky moc i Vám, to jsem potřeboval.

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0