How to Generate Random String in PHP
Are you looking for random, number, lower case upper case or alpha numeric etc using PHP. Just use the Following Short/Simple PHP Snippet/Script. It will help you to Create Random String in PHP.
Random String Generator [ PHP Snippet ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function randomString($length, $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 } |
Call Function
1 2 3 |
<?php echo randomString(10, $type = ''); // It will show 10 digit alpha numeric string. ?> |
Thanks!
Your feedback helps us to improve PHPHive.info