Generate random string
By: Daniel
Have you ever needed to create a random string for use in a project but did not understand how it is done? With the following function you will be able to create random strings of numbers, letters and letters/numbers/symbols at any length.
function randomString($length = '8', $type = '') { // Select which type of characters you want in your random string switch($type) { case 'num': // Use only numbers $salt = '1234567890'; break; case 'lower': // Use only lowercase letters $salt = 'abcdefghijklmnopqrstuvwxyz'; break; case 'upper': // Use only uppercase letters $salt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; default: // Use uppercase, lowercase, numbers, and symbols $salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$*#@!?'; break; } $rand = ''; $i = 0; while ($i < $length) { // Loop until you have met the length $num = rand() % strlen($salt); $tmp = substr($salt, $num, 1); $rand = $rand . $tmp; $i++; } return $rand; // Return the random string }
Some examples on how to use this would be:
echo 'Random 8 character lowercase, uppercase, numbers and symbol string: <strong>'.randomString('8').'</strong><br />'; echo 'Random 12 numbers: <strong>'.randomString('12', 'num').'</strong><br />'; echo 'Random 10 character lowercase string: <strong>'.randomString('10', 'lower').'</strong>';
This would produce:
Random 8 character lowercase, uppercase, numbers and symbol string: tGPQNS0U
Random 12 numbers: 416387690323
Random 10 character lowercase string: pknqxkjsxa
To modify this code you can add extra case sections to modify what characters are added to the random string.





















June 29th, 2011 at 4:21 am
One thing I like about reading through websites like this, is the fact that there aren’t any spelling or even lexical mistakes! Makes it tough on the readers occasionally. Very good work upon that and additionally the topic of this site. Many thanks!
July 18th, 2011 at 4:19 am
I had been thankful to secure a phone coming from my pal while he discovered quite recommendations discussed on the webpage. Dealing with your blog post is actually a great encounter. We appreciate you considering viewers like me, and that i desire for you the greatest associated with success like a expert area.
August 6th, 2011 at 9:29 am
more videos:http://www.ccompliquez.com