Created
February 2, 2021 14:28
-
-
Save gharabaghi/9d72669dec5726058490652a3d6705cb to your computer and use it in GitHub Desktop.
php generate 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
public static function generateRandomPassword($length) | |
{ | |
$signs = '_-!#@'; | |
$signsLength = strlen($signs); | |
$numbers = '0123456789'; | |
$numbersLength = strlen($numbers); | |
$alphs = 'abcdefghijklmnopqrstuvwxyx'; | |
$alphsLength = strlen($alphs); | |
$signsCount = 1; | |
$numbersCount = 2; | |
$alphsCount = $length - $signsCount - $numbersCount; | |
$baseString = ''; | |
for ($i = 0; $i < $alphsCount; $i++) { | |
$baseString .= $alphs[rand(0, $alphsLength - 1)]; | |
} | |
for ($i = 0; $i < $numbersCount; $i++) { | |
$baseString = substr_replace($baseString, $numbers[rand(0, $numbersLength) - 1], rand(0, strlen($baseString)), 0); | |
} | |
for ($i = 0; $i < $signsCount; $i++) { | |
$baseString = substr_replace($baseString, $signs[rand(0, $signsLength) - 1], rand(0, strlen($baseString)), 0); | |
} | |
return $baseString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment