Jragon Posted July 23, 2010 Share Posted July 23, 2010 Hey guys, I getting a problem with my ranbom string crater My function: function randstr($length){ $characters = '0123456789QWERTYUIOPLKJHGFDSAZXCVBNM<>?:@~}{+_)(*&^%\$£!¬`-=[\'],./¦'; $string =''; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return $string; } What happens: when i spefafi the length it dosent allwas keep to that length(the length is 55 chariters) Also what i want it to do is only have 1 of each chariters. Thanks Jragon Quote Link to comment https://forums.phpfreaks.com/topic/208710-random-string-function-not-working-correctly/ Share on other sites More sharing options...
premiso Posted July 23, 2010 Share Posted July 23, 2010 function randstr($length) { $characters = '0123456789QWERTYUIOPLKJHGFDSAZXCVBNM<>?:@~}{+_)(*&^%\$£!¬`-=[\'],./¦'; $characters = join($characters); $string = ""; while (strlen($string) < $length) { $string .= $characters[array_rand($characters, 1)]; } return $string; } Not really sure why your way was not working, but the above should work. Quote Link to comment https://forums.phpfreaks.com/topic/208710-random-string-function-not-working-correctly/#findComment-1090381 Share on other sites More sharing options...
Jragon Posted July 23, 2010 Author Share Posted July 23, 2010 I'm getting this: Warning: array_rand() expects parameter 1 to be array, null given in C:\xampp\htdocs\makecode\includes\inlude.php on line 202 Thanks Jragon Quote Link to comment https://forums.phpfreaks.com/topic/208710-random-string-function-not-working-correctly/#findComment-1090391 Share on other sites More sharing options...
AbraCadaver Posted July 23, 2010 Share Posted July 23, 2010 I'll look at this later as it can probably be simplified, but for now: function randstr($length) { $characters = str_split('0123456789QWERTYUIOPLKJHGFDSAZXCVBNM<>?:@~}{+_)(*&^%\$£!¬`-=[\'],./|'); $string = implode(array_intersect_key($characters, array_flip(array_rand($characters, $length)))); return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/208710-random-string-function-not-working-correctly/#findComment-1090434 Share on other sites More sharing options...
AbraCadaver Posted July 24, 2010 Share Posted July 24, 2010 Simplified somewhat, but you need a condition in there in case the $length is 0 or greater than the number of characters. I'm not sure what you want there (return false, return a string with a maximum length of the characters, etc.): function randstr($length) { $characters = '0123456789QWERTYUIOPLKJHGFDSAZXCVBNM<>?:@~}{+_)(*&^%\$£!¬`-=[\'],./|'; $string = implode(array_rand(array_flip(str_split($characters)), $length)); return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/208710-random-string-function-not-working-correctly/#findComment-1090660 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.