johnmerlino Posted March 2, 2011 Share Posted March 2, 2011 Hey all, I'm looking to produce a random 10 character string like: abcABC0123 when the below method is called: <?php function randomize(){ $chars = explode(' ',(range("a","z"))) . explode(' ',(range("A","Z"))) . explode(' ',(range("0","9"))); $pass = ""; for($i = 0; $i < $len; $i += 1){ array_push($pass,$chars[rand(count($chars)-1)]); } return $pass; } var_dump(randomize(10)); ?> But the randomize method is not returning desired result. Any idea what I'm doing wrong? Thanks for response. Link to comment https://forums.phpfreaks.com/topic/229337-get-a-random-10-character-string/ Share on other sites More sharing options...
sunfighter Posted March 2, 2011 Share Posted March 2, 2011 I ran the code and received this error: Warning: explode() expects parameter 2 to be string, array given ....... That's what your doing wrong. Link to comment https://forums.phpfreaks.com/topic/229337-get-a-random-10-character-string/#findComment-1181668 Share on other sites More sharing options...
kenrbnsn Posted March 2, 2011 Share Posted March 2, 2011 Try this for your function: <?php function randomize(){ $chars = array_merge(range("a","z"),range("A","Z"),range("0","9")); shuffle($chars); return implode('',array_slice($chars,0,10)); } echo randomize(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/229337-get-a-random-10-character-string/#findComment-1181679 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.