Jump to content


Photo

Random Alphanumerical String


  • Please log in to reply
6 replies to this topic

#1 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 27 March 2006 - 09:03 PM

Hey,

Does anybody know a simple script to randomize a string of random letters and numbers, like so: $string = "abcdefghijklmnopqrstuvwxyz0123456789";. I thought rand() would've done the job, but that only works with numbers and then I tried str_shuffle() but that only randomizes the full string, and I want to limit my string to 32 characters (well any really, but i'm using 32 for what I need).

If anyone can help, that'd be great, thanks.

Chris.

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 27 March 2006 - 09:09 PM

See second line code example at:

[a href=\"http://us3.php.net/manual/en/function.uniqid.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.uniqid.php[/a]

#3 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 27 March 2006 - 09:20 PM

[!--quoteo(post=359033:date=Mar 27 2006, 09:09 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Mar 27 2006, 09:09 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
See second line code example at:

[a href=\"http://us3.php.net/manual/en/function.uniqid.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.uniqid.php[/a]
[/quote]

Cool, thanks.

Is their a way to change the limit as to what I random, like that function provides 32 characters, but what about, say for example, I wanted it to just provide a random 5 character string?

Thanks.

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 27 March 2006 - 09:34 PM

Let it generate the 32 characters, then you simply use substr() to grab as many characters as you want. See:

[a href=\"http://us3.php.net/manual/en/function.substr.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.substr.php[/a]



#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 27 March 2006 - 10:36 PM

MD5 isn't really a random string generator, though it can serve that purpose fairly well. In case you desire a truly random distribution, you can use this function I wrote a while back:

function generatestring($len = 12) {
    $chars = "abcdefghijklmnopqrstuvwxyz";
    $chars .= strtoupper($chars);
    $chars .= "0123456789";
    $ch = str_split($chars);
    $max = count($ch) - 1;
    for ($i = 0; $i < $len; $i++) {
        $return .= $ch[mt_rand(0,$max)];
    }
    return $return;
}


#6 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 27 March 2006 - 11:24 PM

Thanks for your help guys. From the stuff you told me and the links you provided, I came up with this:

<?php
$string = "qwertyuiopasdfghjklzxcvbnm0123456789";
echo str_shuffle(substr($string, 0, 32));
?>

It's small, but does the job. Simple.

#7 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 27 March 2006 - 11:28 PM

[!--quoteo(post=359053:date=Mar 27 2006, 02:36 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 27 2006, 02:36 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
MD5 isn't really a random string generator, though it can serve that purpose fairly well. In case you desire a truly random distribution, you can use this function I wrote a while back:

function generatestring($len = 12) {
    $chars = "abcdefghijklmnopqrstuvwxyz";
    $chars .= strtoupper($chars);
    $chars .= "0123456789";
    $ch = str_split($chars);
    $max = count($ch) - 1;
    for ($i = 0; $i < $len; $i++) {
        $return .= $ch[mt_rand(0,$max)];
    }
    return $return;
}
[/quote]
FYI:

The str_split() function is only available in PHP 5+.

You ideally should initialize the $return variable before using it with ".=".

You can abbreviate the first four lines to this one line:

$ch = str_split(range('a', 'z') . range('A', 'Z') . range(0, 9));

You actually don't even need the str_split() because you can access individual characters from a string using $string{index} syntax.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users