Skip to content

Instantly share code, notes, and snippets.

@bitbonsai
Created May 23, 2014 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitbonsai/e9c814d06d31ad1d8e41 to your computer and use it in GitHub Desktop.
Save bitbonsai/e9c814d06d31ad1d8e41 to your computer and use it in GitHub Desktop.
Password Strenght PHP
function scorePassword($password){
$score=0;
if(!$password)
return 0;
// award every unique letter until 5 repetitions
$letters=str_split($password);
$scores=array();
foreach($letters as $letter){
$scores[$letter]=(isset($scores[$letter]))?$scores[$letter]+1:1;
$score+=5/$scores[$letter];
}
// bonus points for mixing it up
$variations = array(
preg_match('/\d/', $this->password),
preg_match('/[a-z]/', $this->password),
preg_match('/[A-Z]/', $this->password),
preg_match('/\W/', $this->password),
);
$variationCount=0;
foreach($variations as $check){
$variationCount+=($check)?1:0;
}
$score+=($variationCount-1)*10;
return round($score);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment