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
//große & kleine Buchstaben + Zahlen, beliebige Größe, min 10 Zeichen, max 20 Zeichen
echo getRandomString(array('lowtypes', 'bigtypes', 'nummerics'), false, 20, 10);

//große Buchstaben + Zahlen, Fixe Größe von 15 Zeichen
echo getRandomString(array('bigtypes', 'nummerics'), true, 15);
Bookmarke diesen Post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • blogmarks
  • LinkArena
  • Live
  • MisterWong.DE
  • MSN Reporter
  • MySpace
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Yigg

Schlagworte: , ,

Kommentieren