random alphanumeric code generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myheadhurts
Forum Newbie
Posts: 1
Joined: Mon Jun 12, 2006 10:08 am

random alphanumeric code generation

Post by myheadhurts »

hi all,

my first post here.

I have searched the index, but can't find how to do this


I am rewriting an old perl script that had these two lines

my @chars=('A'..'Z','a'..'z','0'..'9');
my $reference = join'',map $chars[rand @chars],1..14;

and i'd like to rewrite the same thing in php - but do I need to make an array, select elements an join them or is php cleverer than that???? Is there a standard way of doing this? Is there even a php makerandomalphanumericcode() function (that wold be nice)

thanks for any help!

Jules
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I use this to make a code without the letters and Numbers: I,O,Q,1,0,L and I think a few other ones that look a like.

Code: Select all

$code = ''; 
        $chars = array(50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90); 
		$chars = array_map('chr', $chars); 
        $pool = implode('', $chars); 
		$size = strlen($pool) - 1; 
        for($i = 0; $i < $len; ++$i) 
        { 
          $code .= $pool[mt_rand(0, $size)];
If you want to change it you just need to add the character number or change them to have the numbers that match the ASCII numbers on: http://www.lookuptables.com/
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

tecktalkcm0391 wrote:I use this to make a code without the letters and Numbers: I,O,Q,1,0,L and I think a few other ones that look a like.

Code: Select all

$code = ''; 
        $chars = array(50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90); 
		$chars = array_map('chr', $chars); 
        $pool = implode('', $chars); 
		$size = strlen($pool) - 1; 
        for($i = 0; $i < $len; ++$i) 
        { 
          $code .= $pool[mt_rand(0, $size)];
Why don't you just do "$pool = '23456789ABCDEFGHJKMNPRSTUVWXYZ';" instead of mucking about with array_map() and stuff?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

I'm lazy, I just make a sha1()/sha256() call across the output from uniqid() and select a substring for the proper length...
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

onion2k wrote:
tecktalkcm0391 wrote:I use this to make a code without the letters and Numbers: I,O,Q,1,0,L and I think a few other ones that look a like.

Code: Select all

$code = ''; 
        $chars = array(50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90); 
		$chars = array_map('chr', $chars); 
        $pool = implode('', $chars); 
		$size = strlen($pool) - 1; 
        for($i = 0; $i < $len; ++$i) 
        { 
          $code .= $pool[mt_rand(0, $size)];
Why don't you just do "$pool = '23456789ABCDEFGHJKMNPRSTUVWXYZ';" instead of mucking about with array_map() and stuff?
I dunno, thats what came out of viewtopic.php?t=49300
Post Reply