forumnz Posted January 27, 2008 Share Posted January 27, 2008 How can I generate a random 35 length variable using numbers and letters (lower and upper case)? Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/ Share on other sites More sharing options...
laffin Posted January 27, 2008 Share Posted January 27, 2008 plenty of different ways for this question think of valid chars as cards. u can have it do one of 2 methods 1) Just use the cards, no repeats of cards, shuffle and deal 35 cards. 2) Put the cards in seperate piles, and open a few more decks and put them on each pile. pick a random pile, draw a card, and repeat 34 more times. this one is simple to do. Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/#findComment-450320 Share on other sites More sharing options...
forumnz Posted January 27, 2008 Author Share Posted January 27, 2008 I suppose I get what you mean, but I really don't know where to begin? Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/#findComment-450325 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 Here's a quick way of doing it but only the last three characters might be in uppercase. You can change it to pick a random number of characters in the token to change to uppercase. <?php $token = substr(md5(uniqid(rand(), true)) . strtoupper(md5(uniqid(rand(), true))), 0, 35); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/#findComment-450329 Share on other sites More sharing options...
ziv Posted January 27, 2008 Share Posted January 27, 2008 something like this? <?php /** * @param int $length - max 40 chars (sha1 length) * @return string */ function getRandomPassword($length) { return substr( base64_encode( sha1( mt_rand() ) ), 0, $length); } // i used base64 encoding becouse it's convert any // data to letters (lowers and uppers) and numbers echo getRandomPassword(35); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/#findComment-450332 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 ziv, that's nice but base64_encode() could return non-alphanumeric characters (such as "="). Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/#findComment-450335 Share on other sites More sharing options...
ziv Posted January 27, 2008 Share Posted January 27, 2008 only on the end of the string. sha1() return 40 chars and base64_encode() increase the numbers of chars. that is the reason i wrote the function is limited to 40 chars length. Quote Link to comment https://forums.phpfreaks.com/topic/88015-random-35-character-word/#findComment-450342 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.