create a password with at least one uppercase letter in php

To create a password with at least one uppercase letter, you can use the following PHP code:

main.php
$password = "myPassword123";
$uppercase = preg_match('@[A-Z]@', $password);

if(!$uppercase) {
    // The password doesn't contain an uppercase letter
    // Here, you can generate a new password or add an uppercase letter to the password
    $password .= "A";
}

// Use the $password variable for further processing, such as storing it in a database or displaying it to the user
380 chars
11 lines

In this code, we use a regular expression to check if the password contains at least one uppercase letter. The preg_match() function searches for the pattern [A-Z] (an uppercase letter) in the $password variable. If the pattern is not found, we add an uppercase letter "A" to the end of the password string.

You can adjust the regular expression pattern to match any other character requirements for your password as well.

gistlibby LogSnag