Last active
January 8, 2016 02:22
-
-
Save huzemin/85c8e688e317a30be147 to your computer and use it in GitHub Desktop.
Generate random string ( use to generate a password)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!function_exists('generate_random_str')) { | |
function generate_random_str($len = 9, $type=2) { | |
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | |
$numbers = '0123456789'; | |
$special_chars = '!@#$%^&*()-_ []{}<>~`+=,.;:/?|'; | |
$words = ''; | |
switch ($type) { | |
case 1: | |
$words = $letters.$numbers; | |
break; | |
case 2: | |
$words = $letters.$numbers.$special_chars; | |
break; | |
case 3: | |
$words = $letters; | |
break; | |
case 4: | |
$words = $numbers; | |
break; | |
default: | |
$words = $letters.$numbers; | |
break; | |
} | |
$words_len = strlen($words); | |
$temp = array(); | |
for($i = 0; $i < $len; $i++) { | |
$temp[] = $words[mt_rand(0, $words_len-1)]; | |
} | |
return join('', $temp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment