password generator with 5 letters, 3 numbers and one special character in php

main.php
function generatePassword() {
    // Set of characters to be used in password
    $chars = "abcdefghijklmnopqrstuvwxyz";
    $nums = "0123456789";
    $specialChars = "!@#$%^&*()_-=+{}[]";

    // Generate random strings of required length
    $letters = substr(str_shuffle($chars), 0, 5);
    $numbers = substr(str_shuffle($nums), 0, 3);
    $specialChar = substr(str_shuffle($specialChars), 0, 1);

    // Combine all the generated strings
    $password = $letters . $numbers . $specialChar;

    // Shuffle the final password
    $password = str_shuffle($password);

    return $password;
}

echo "Generated Password: " . generatePassword();
645 chars
22 lines

The generatePassword() function generates a random string of 5 letters, 3 numbers and 1 special character. It first defines sets of characters to be used in the password, generates random strings of required length for each set, combines them and shuffles the final password.

gistlibby LogSnag