Code-Snippet: getRandomString();
Heute gibt es einen kleinen Code-Snippet, welcher einen zufällig generierten Code zurückgibt.
Die Funktion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | function getRandomString($types = array('lowtypes', 'bigtypes', 'nummerics'), $fix = false, $max = 20, $min = 1) { $strings = ""; $output = ""; foreach($types as $type) { switch($type) { case 'lowtypes': $strings .= 'abcdefghijklmnopqrstuvwxyz'; break; case 'bigtypes': $strings .= 'ABCDEFGHJIKLMNOPQRSTUVWXYZ'; break; case 'nummerics': $strings .= '0123456789'; break; } } $letters = (!$fix) ? rand($min, $max) : $max; for($i = 0; $i < $letters; $i++) { $output .= $strings[rand(0, strlen($strings) - 1)]; } return $output; } |
Funktionsaufruf:
1 2 3 4 5 |