[PHP] randomly generate alphanumeric charachters
05-17-2005, 08:24 AM
|
[PHP] randomly generate alphanumeric charachters
|
Posts: 177
Location: GA
|
basically, i need some code that will randomly generate alphanumeric charachters
it will be at least 4 charachters long and a maximum or 15 charachters long.
then assign it to a variable like $random
so how would i do this?
|
|
|
|
05-17-2005, 08:35 AM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
Something like this: ?
PHP Code:
<?php
$numchars = rand(4,15);
$chars = explode(',','a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9');
$random='';
for($i=0; $i<$numchars;$i++) {
$random.=$chars[rand(0,count($chars)-1)];
}
echo $random;
?>
Ignore spaces in the alphabet array, the php syntax highlighter puts them in on long lines for some unknown reason...
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
Last edited by 0beron; 05-17-2005 at 08:46 AM..
|
|
|
|
05-17-2005, 11:34 PM
|
KeyCodes
|
Posts: 15
Location: New Jersey
|
You can also use keycodes for random letters and numbers
A reference for ASCII Key Codes
PHP Code:
<?php
$pwd=""; // to store generated password
for($i=0;$i<15;$i++)
{
$num=rand(48,122);
if(($num > 97 && $num < 122))
{
$pwd.=chr($num);
}
else if(($num > 65 && $num < 90))
{
$pwd.=chr($num);
}
else if(($num >48 && $num < 57))
{
$pwd.=chr($num);
}
else if($num==95)
{
$pwd.=chr($num);
}
else
{
$i--;
}
}
echo $pwd; // prints the password
?>
|
|
|
|
05-17-2005, 11:59 PM
|
|
Posts: 3,110
Location: Toronto, Ontario
|
Since we're sharing techniques...
I usually use curly brackets with a string of characters, instead of exploding into an array:
PHP Code:
// Instead of using arrays
$chars = explode(' ', 'a b c d e f g');
$e = $chars[4];
// You can use curlies
$chars = 'abcdefg';
$e = $chars{4};
In this case, something like this might do:
PHP Code:
// Examples
echo randomStr() . "<br />\n"; // HxaScR4B
echo randomStr(10) . "<br />\n"; // PJzQ0T7qvJ5BU0
echo randomStr(10, 30) . "<br />\n"; // TkVcembw642Et4aVOrPb5NIM
echo randomStr(10, 30, 'dsjklfuds87') . "<br />\n"; // d8ldsldu7kkjskdjsuuff7
function randomStr($min_chars = 4, $max_chars = 15, $use_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
$num_chars = rand($min_chars, $max_chars);
$num_usable = strlen($use_chars) - 1;
$string = '';
for($i = 0; $i < $num_chars; $i++)
{
$rand_char = rand(0, $num_usable);
$string .= $use_chars{$rand_char};
}
return $string;
}
Quote:
|
Ignore spaces in the alphabet array, the php syntax highlighter puts them in on long lines for some unknown reason...
|
vB puts a space in every 80 chars to stop people from inserting huge lines that stretch the post tables.
|
|
|
|
05-18-2005, 08:23 AM
|
|
Posts: 177
Location: GA
|
thanks all, these are jsut what i wanted
|
|
|
|
|
« Reply to [PHP] randomly generate alphanumeric charachters
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|