Skip to content

Instantly share code, notes, and snippets.

@jorijn
Last active December 15, 2015 03:29
Show Gist options
  • Save jorijn/5194678 to your computer and use it in GitHub Desktop.
Save jorijn/5194678 to your computer and use it in GitHub Desktop.
Create a password rating
<?php
/**
* test - score: 4
* tESt - score: 4
* TeSt - score: 4
* TeS342 - score: 12
* T34@$sTD - score: 72
* :saR%8jmc9UjeK - score: 504
*/
public function get_password_score($password, $requirement = 30, $min_characters = 8)
{
$uppercase_score = (($strength = strlen(preg_replace('/[^A-Z]/', '', $password)) * 1) > 0 ? $strength : 1);
$lowercase_score = (($strength = strlen(preg_replace('/[^a-z]/', '', $password)) * 1) > 0 ? $strength : 1);
$numeric_score = (($strength = strlen(preg_replace('/[^0-9]/', '', $password)) * 2) > 0 ? $strength : 1);
$character_score = (($strength = strlen(preg_replace('/[a-zA-Z0-9]/', '', $password)) * 3) > 0 ? $strength : 1);
$total_score = $lowercase_score * $uppercase_score * $numeric_score * $character_score;
if (strlen($password) < $min_characters && $total_score > $requirement)
return ($requirement - 1); // push them towards more characters by not going over the minimum
return $total_score;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment