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
random alphanumeric code generation
Moderator: General Moderators
-
myheadhurts
- Forum Newbie
- Posts: 1
- Joined: Mon Jun 12, 2006 10:08 am
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
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.
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/
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?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)];
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
I dunno, thats what came out of viewtopic.php?t=49300onion2k wrote:Why don't you just do "$pool = '23456789ABCDEFGHJKMNPRSTUVWXYZ';" instead of mucking about with array_map() and stuff?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)];