You could do something like:
<?php
/**
* @author Charles Reace
* @copyright 2008
*/
function getPasswordStrength($password, $minLength, $maxLength)
{
$strength = 0;
$patterns = array(
'/[a-z]/',
'/[A-Z]/',
'/[0-9]/',
'/[' . preg_quote('-!@#$%^&*()_=+[{]}') . ']/' // make sure "-" is first
);
$length = strlen($password);
if($length >= $minLength && $length <= $maxLength)
{
$strength++;
}
else
{
return 0;
}
foreach($patterns as $regexp)
{
if(preg_match($regexp, $password))
$strength++;
}
return $strength;
}
// TEST:
$test = array(
'abcdabcdabcd',
'12341234ABCD',
'abcABC1234',
'abcABC123!@#',
'Aa1!',
'abcABC123!@#abcABC123!@#',
);
echo "<pre>";
foreach($test as $pwd)
{
echo $pwd . ' - ' . getPasswordStrength($pwd, 8, 16) . "\n";
}
echo "</pre>";
?>
Output:
abcdabcdabcd - 2
12341234ABCD - 3
abcABC1234 - 4
abcABC123!@# - 5
Aa1! - 0
abcABC123!@#abcABC123!@# - 0