SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    generating a random alphanumeric code

    whats the best way to generate a random, alphanumeric, upper and lowercase string?

    thanks
    eric

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mt_rand

    Be sure to check out the examples in the documentation, as it shows you how to use this to generate a random string of letters and numbers.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I normally use something like this:
    PHP Code:
     function random_string($length) {
         
    $array array_merge(range('a','z'), range('A','Z'));
     
         
    $string '';
         for(
    $i=1;$i<=$length;$i++) 
             
    $string .= $array[mt_rand(0,51)];
     
         return 
    $string;
     
     } 
    Codyrobert.com - Designer and Developer

  4. #4
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by codyrockx
    I normally use something like this:
    PHP Code:
     function random_string($length) {
         
    $array array_merge(range('a','z'), range('A','Z'));
     
         
    $string '';
         for(
    $i=1;$i<=$length;$i++) 
             
    $string .= $array[mt_rand(0,51)];
     
         return 
    $string;
     
     } 
    that doesnt seem to generate any numbers. is it supposed to?

  5. #5
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, forgot the numbers then you'd change this line
    PHP Code:
      $array array_merge(range('a','z'), range('A','Z'), range(0,9)); 
    Codyrobert.com - Designer and Developer

  6. #6
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,001
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I use something like this to generate a random alphanumeric string.

    PHP Code:
    $string substr(md5(uniqid(rand(),1)), 05); 
    Of course you can modify substr call to suit your needs.
    -- Jelena --


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •