karimali831 Posted April 13, 2011 Share Posted April 13, 2011 Hi, I'm having trouble generating random string with 8 characters, when I refresh I sometimes get 6 characters and sometimes 8. "-" not included as character. Wrong: 6JW-F7W Correct: (should always generate random like this) DVMF-F36V I know I'm trying to achieve this in wrong methods but it's the only way I know. Hope someone can help. function getReference() { $length = 8; $characters = "0123456789abcdefghijklmnopqrstuvwxyz"; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return strtoupper(substr($string, 0, -$length/2)."-".substr($string, $length/2-1, -1)); } Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/ Share on other sites More sharing options...
drisate Posted April 13, 2011 Share Posted April 13, 2011 <?php function getReference($size = 4) { $size = $size > 36 ? 30 : $size; $pool = array_merge(range(0, 9), range('a', 'z')); $rand_keys = array_rand($pool, $size); $random = ''; foreach ($rand_keys as $key) { $random .= $pool[$key]; } return $random ; } echo getReference(4).'-'.getReference(4); ?> Something like this? Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201230 Share on other sites More sharing options...
kenrbnsn Posted April 13, 2011 Share Posted April 13, 2011 You can also use the shuffle and array_slice functions: <?php function getReference($size = 4) { $pool = array_merge(range(0, 9), range('a', 'z')); // // if you want to include uppercase letters, change the above line to // $pool = array_merge(range(0,9),range('a','z'),range('A','Z')); $size = $size > count($pool) ? 30 : $size; // base it on the size of the array of possible values, so if you add more this doesn't have to change shuffle($pool); return (implode('',array_slice($pool,0,$size))); } echo getReference(4).'-'.getReference(4); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201246 Share on other sites More sharing options...
gergy008 Posted April 13, 2011 Share Posted April 13, 2011 I would tend to use the MD5 function to generate random characters $str1=substr(md5(rand()*rand()/rand(), rand(1, 20), 4); $str2=substr(md5(rand()*rand()/rand(), rand(1, 20), 4); $str3=strtoupper($str1."-".$str2); Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201282 Share on other sites More sharing options...
kenrbnsn Posted April 13, 2011 Share Posted April 13, 2011 The problem with using MD5 is that it returns a hexadecimal number (characters 0 - F) and not the full alphabet like the OP was using. BTW, the code you posted is missing two ")"... It could have been written as <?php function gr($size=4) { return (strtoupper(substr(md5(rand()*rand()/rand()), rand(1, 20), $size) . '-' . substr(md5(rand()*rand()/rand()), rand(1, 20),$size))); } echo gr(); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201290 Share on other sites More sharing options...
gergy008 Posted April 13, 2011 Share Posted April 13, 2011 The problem with using MD5 is that it returns a hexadecimal number (characters 0 - F) and not the full alphabet like the OP was using. BTW, the code you posted is missing two ")"... It could have been written as <?php function gr($size=4) { return (strtoupper(substr(md5(rand()*rand()/rand()), rand(1, 20), $size) . '-' . substr(md5(rand()*rand()/rand()), rand(1, 20),$size))); } echo gr(); ?> Ken Haha, Thanks, I just realised. It was intended to be an example though but yeah I suppose. Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201292 Share on other sites More sharing options...
karimali831 Posted April 14, 2011 Author Share Posted April 14, 2011 Forgot to say thanks alot, works great Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201668 Share on other sites More sharing options...
maxudaskin Posted April 14, 2011 Share Posted April 14, 2011 Forgot to say thanks alot, works great At the bottom of the page, click "Topic Solved" Quote Link to comment https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201711 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.