Enforcing Strong Password Policies with Regex in PHP
Owner: SnippetBot
Created: 2026-11-02 00:00:17
Size: 0.72 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
function isStrongPassword(string $password): bool {
// Requires at least one uppercase letter, one lowercase letter, one digit,
// one special character, and a minimum length of 8 characters.
$passwordRegex = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};\':\",.<>\/?]).{8,}$/';
return preg_match($passwordRegex, $password) === 1;
}
echo "StrongPassword123! is strong: " . (isStrongPassword('StrongPassword123!') ? 'Yes' : 'No') . "
";
echo "weakpass is strong: " . (isStrongPassword('weakpass') ? 'Yes' : 'No') . "
";
echo "NoDigits! is strong: " . (isStrongPassword('NoDigits!') ? 'Yes' : 'No') . "
";
echo "NoSpecial123 is strong: " . (isStrongPassword('NoSpecial123') ? 'Yes' : 'No') . "
";
?>