Skip to content

Instantly share code, notes, and snippets.

@lukasni
Last active March 10, 2016 21:56
Show Gist options
  • Save lukasni/d3ba2345134ff4e5c805 to your computer and use it in GitHub Desktop.
Save lukasni/d3ba2345134ff4e5c805 to your computer and use it in GitHub Desktop.
<?php
// Password generator function
function passwordGen($num, $length, $inclBigLetters = true, $inclSmallLetters = true, $inclNumbers = true, $inclSpecialChars = false) {
$bigLetters = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
$smallLetters = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
$numbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$specialChars = array("$", "_", "+", "-", "/", "*", "=", "(", ")");
$result = array();
$characterSet = array();
if ($inclSmallLetters) { $characterSet = array_merge($characterSet, $smallLetters);}
if ($inclBigLetters) { $characterSet = array_merge($characterSet, $bigLetters);}
if ($inclNumbers) { $characterSet = array_merge($characterSet, $numbers);}
if ($inclSpecialChars) { $characterSet = array_merge($characterSet, $specialChars);}
for ($i = 0; $i < $num; $i++) {
$text = "";
for ($chars = 0; $chars < $length; $chars++) {
$text .= $characterSet[random_int(0,count($characterSet)-1)];
}
$result[] = $text;
}
return $result;
}
// Testing
$test = passwordGen(5,32);
var_dump($test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment