CMellor Posted March 27, 2006 Share Posted March 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/ Share on other sites More sharing options...
toplay Posted March 27, 2006 Share Posted March 27, 2006 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 Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/#findComment-21337 Share on other sites More sharing options...
CMellor Posted March 27, 2006 Author Share Posted March 27, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/#findComment-21340 Share on other sites More sharing options...
toplay Posted March 27, 2006 Share Posted March 27, 2006 Let it generate the 32 characters, then you simply use substr() to grab as many characters as you want. See:[a href=\"http://us3.php.net/manual/en/function.substr.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.substr.php[/a] Quote Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/#findComment-21346 Share on other sites More sharing options...
wickning1 Posted March 27, 2006 Share Posted March 27, 2006 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 Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/#findComment-21357 Share on other sites More sharing options...
CMellor Posted March 27, 2006 Author Share Posted March 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/#findComment-21371 Share on other sites More sharing options...
toplay Posted March 27, 2006 Share Posted March 27, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/5964-random-alphanumerical-string/#findComment-21372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.