Skip to content

Instantly share code, notes, and snippets.

@huzemin
Last active January 8, 2016 02:22
Show Gist options
  • Save huzemin/85c8e688e317a30be147 to your computer and use it in GitHub Desktop.
Save huzemin/85c8e688e317a30be147 to your computer and use it in GitHub Desktop.
Generate random string ( use to generate a password)
<?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