<?php
function scorePassword(string $pw): array {
if ($pw === '') return [0, 'Empty'];
$len = strlen($pw);
$score = 0;
if ($len >= 8) $score++;
if ($len >= 12) $score++;
if ($len >= 16) $score++;
if (preg_match('/[A-Z]/', $pw) && preg_match('/[a-z]/', $pw) && preg_match('/\d/', $pw)) $score++;
if (preg_match('/[^A-Za-z0-9]/', $pw)) $score++;
static $blocklist = ['password','12345678','qwerty','letmein','welcome','admin','iloveyou'];
if (in_array(strtolower($pw), $blocklist, true)) return [0, 'Common password — pick another'];
$score = min($score, 4);
$label = ['Very weak','Weak','OK','Strong','Very strong'][$score];
return [$score, $label];
}
print_r(scorePassword('hunter2')); // [0, "Very weak"]
print_r(scorePassword('Tr0ub4dor&3')); // [3, "Strong"]
print_r(scorePassword('correct horse battery staple')); // [4, "Very strong"]
Create a free account and build your private vault. Share publicly whenever you want.