Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
[PHP] randomly generate alphanumeric charachters
Old 05-17-2005, 08:24 AM [PHP] randomly generate alphanumeric charachters
HitRaj47's Avatar
Extreme Talker

Posts: 177
Location: GA
Trades: 0
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?
HitRaj47 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-17-2005, 08:35 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 05-17-2005, 11:34 PM KeyCodes
Average Talker

Posts: 15
Location: New Jersey
Trades: 0
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
?>
wbarrett26 is offline
Reply With Quote
View Public Profile Visit wbarrett26's homepage!
 
Old 05-17-2005, 11:59 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
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(1030)                  . "<br />\n";   // TkVcembw642Et4aVOrPb5NIM
echo randomStr(1030'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.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 05-18-2005, 08:23 AM
HitRaj47's Avatar
Extreme Talker

Posts: 177
Location: GA
Trades: 0
thanks all, these are jsut what i wanted
HitRaj47 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to [PHP] randomly generate alphanumeric charachters
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17032 seconds with 12 queries