• home
  • forum
  • my
  • kt
  • download
  • Create random strings

    Author: 2007-08-20 10:23:20 From:

    Hi folks. This isn't really a tutorial, just a handy script that I use a fair bit in my work. It generates a random string of numbers and letters to a length of which you decide. Here is the script in action (refresh the page for another string) and here is the code for the function:

    <?php
    function randStr($num
    ){
        
    $chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9'
    );
        for(
    $i=0;$i<=$num;$i
    ++){
            
    $cs round(rand(1,count($chars
    )));
            
    $up rand(0,2
    );
            if(
    $up<=1
    ){
                
    $str.=strtoupper($chars[$cs
    ]);
            }else{
                
    $str.=$chars[$cs
    ];
            }
        }
        return 
    $str
    ;
    }
    echo 
    randStr(30
    );
    ?>

    So to call it, you'd use something like this:

    echo randStr(30);

    That would display a random string with 30 characters.

    Cheers.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot