To create a password with a custom length in PHP, you can use the str_shuffle()
function to randomize a string of characters and then use substr()
function to get a substring of the length you want to use as a password. The following code snippet demonstrates this:
main.php363 chars12 lines
In this code, we start by defining a string that contains all the characters we want to use in our password. We then define the desired length for the password as $passwordLength
.
We use the str_shuffle()
function to randomize the characters in $charString
and then use substr()
to get a substring of the first $passwordLength
characters in the randomized string.
Finally, we output the resulting password using echo
. This code will generate a random password using the characters specified in $charString
with a length of 8
characters. You can modify the length and characters to suit your specific needs.
gistlibby LogSnag