Jump to content

Recommended Posts

Hey,

Does anybody know a simple script to randomize a string of random letters and numbers, like so: [b]$string = "abcdefghijklmnopqrstuvwxyz0123456789";[/b]. I thought [b]rand()[/b] would've done the job, but that only works with numbers and then I tried [b]str_shuffle()[/b] 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.
Link to comment
https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/
Share on other sites

[!--quoteo(post=359033:date=Mar 27 2006, 09:09 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Mar 27 2006, 09:09 PM) [snapback]359033[/snapback][/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.
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:

[code]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;
}[/code]
Thanks for your help guys. From the stuff you told me and the links you provided, I came up with this:

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

It's small, but does the job. Simple.
[!--quoteo(post=359053:date=Mar 27 2006, 02:36 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 27 2006, 02:36 PM) [snapback]359053[/snapback][/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:

[code]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;
}[/code]
[/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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.