Calculate Password Strength as Score - Simple

/** * @param string $str * * @return float * * Returns a float between 0 and 100. The closer the number is to 100 the * the stronger password is; further from 100 the weaker the password is. */ if(!function_exists('password_strength')) { function password_strength($str) { $h = 0; $size = strlen($str); foreach(count_chars($str, 1) as $v) { $p = $v / $size; $h -= $p * log($p) / log(2); } $strength = ($h / 4) * 100; return ($strength > 100 ? 100 : $strength); } } var_dump(password_strength("Correct Horse Battery Staple")); // float 90.938437781651 echo "<br>"; var_dump(password_strength("Super Monkey Ball")); // float 93.363041619494 echo "<br>"; var_dump(password_strength("Tr0ub4dor&3")); // float 81.940335920478 echo "<br>"; var_dump(password_strength("abc123")); // float 64.624062518029 echo "<br>"; var_dump(password_strength("sweet")); // float 48.048202372184

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.