public static function checkPassword($password, $options = null)
    {
        $options = static::getOptions($options, [
            "length"    =>  8,
            "unique"    =>  4,
            "lowercase" =>  true,
            "uppercase" =>  true,
            "alpha"     =>  true,
            "numeric"   =>  true,
        ]);

        $problems = [];

        $len = mb_strlen($password);

        if ($len < $options["length"]) {
            $problems["length"] = "Passwords must be at least " . $options["length"] . " characters long";
        }

        $unique = [];
        for ($i = 0; $i < $len; ++$i) {
            $char = mb_substr($password, $i, 1);
            if (!in_array($char, $unique)) {
                $unique[] = $char;
            }
        }
        if (count($unique) < $options["unique"]) {
            $problems["unique"] = "Passwords must contain at least " . $options["unique"] . " unique characters";
        }

        if (!preg_match("/[a-z]/", $password)) {
            $problems["lowercase"] = "Passwords must contain at least 1 lowercase letter";
        }
        if (!preg_match("/[A-Z]/", $password)) {
            $problems["uppercase"] = "Passwords must contain at least 1 uppercase letter";
        }
        if (!preg_match("/[a-z]/i", $password)) {
            $problems["alpha"] = "Passwords must contain at least 1 letter";
        }
        if (!preg_match("/[0-9]/", $password)) {
            $problems["numeric"] = "Passwords must contain at least 1 number";
        }

        return $problems;
    }
public static function checkPassword($password, $options = null)
    {
        $options = static::getOptions($options, [
            "length"    =>  8,
            "unique"    =>  4,
            "lowercase" =>  true,
            "uppercase" =>  true,
            "alpha"     =>  true,
            "numeric"   =>  true,
        ]);

        $problems = [];

        $len = mb_strlen($password);

        if ($len < $options["length"]) {
            $problems["length"] = "Passwords must be at least " . $options["length"] . " characters long";
        }

        $unique = [];
        for ($i = 0; $i < $len; ++$i) {
            $char = mb_substr($password, $i, 1);
            if (!in_array($char, $unique)) {
                $unique[] = $char;
            }
        }
        if (count($unique) < $options["unique"]) {
            $problems["unique"] = "Passwords must contain at least " . $options["unique"] . " unique characters";
        }

        if (!preg_match("/[a-z]/", $password)) {
            $problems["lowercase"] = "Passwords must contain at least 1 lowercase letter";
        }
        if (!preg_match("/[A-Z]/", $password)) {
            $problems["uppercase"] = "Passwords must contain at least 1 uppercase letter";
        }
        if (!preg_match("/[a-z]/i", $password)) {
            $problems["alpha"] = "Passwords must contain at least 1 letter";
        }
        if (!preg_match("/[0-9]/", $password)) {
            $problems["numeric"] = "Passwords must contain at least 1 number";
        }

        return $problems;
    }
public static function checkPassword ( $ password , $ options = null ) { $ options = static :: getOptions ( $ options , [ "length" => 8 , "unique" => 4 , "lowercase" => true , "uppercase" => true , "alpha" => true , "numeric" => true , ] ) ; $ problems = [ ] ; $ len = mb_strlen ( $ password ) ; if ( $ len < $ options [ "length" ] ) { $ problems [ "length" ] = "Passwords must be at least " . $ options [ "length" ] . " characters long" ; } $ unique = [ ] ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { $ char = mb_substr ( $ password , $ i , 1 ) ; if ( ! in_array ( $ char , $ unique ) ) { $ unique [ ] = $ char ; } } if ( count ( $ unique ) < $ options [ "unique" ] ) { $ problems [ "unique" ] = "Passwords must contain at least " . $ options [ "unique" ] . " unique characters" ; } if ( ! preg_match ( "/[a-z]/" , $ password ) ) { $ problems [ "lowercase" ] = "Passwords must contain at least 1 lowercase letter" ; } if ( ! preg_match ( "/[A-Z]/" , $ password ) ) { $ problems [ "uppercase" ] = "Passwords must contain at least 1 uppercase letter" ; } if ( ! preg_match ( "/[a-z]/i" , $ password ) ) { $ problems [ "alpha" ] = "Passwords must contain at least 1 letter" ; } if ( ! preg_match ( "/[0-9]/" , $ password ) ) { $ problems [ "numeric" ] = "Passwords must contain at least 1 number" ; } return $ problems ; }
Check if a password conforms to the specified complexitiy rules.
If the password passes all tests then the function returns an empty array.
Otherwise it returns an array of all the checks that failed.

$options:
- int "length" The minimum length the password must be (default: 8)
- int "unique" The number of unique characters the password must contain (default: 4)
- bool "lowercase" Whether the password must contain lowercase letters (default: true)
- bool "uppercase" Whether the password must contain uppercase letters (default: true)
- bool "alpha" Whether the password must contain letters (default: true)
- bool "numbers" Whether the password must contain numbers (default: true)

@param string $password The password to check
@param array $options An array of options (see above)

@return string[]
Check if a password conforms to the specified complexitiy rules . If the password passes all tests then the function returns an empty array . Otherwise it returns an array of all the checks that failed .