Created
March 21, 2017 10:05
-
-
Save flowtwo/b5a99d62da22783b0d71360452d689d7 to your computer and use it in GitHub Desktop.
Validate Danish CPR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Validate CPR-nr. | |
function is_cpr_valid($cpr) { | |
$cleaned = preg_replace("/[^0-9]/", "", $cpr); | |
if (strlen($cleaned) != 10) { | |
return FALSE; | |
} | |
$valid_dates = Array('010165', '010166', '010170', '010180', '010187', '010190'); | |
if (in_array(substr($cleaned, 0, 6), $valid_dates)) { | |
return TRUE; | |
} | |
$cpr_arr = str_split($cleaned, 1); | |
$mul_arr = str_split("4327654321", 1); | |
$sum = 0; | |
for ($i = 0; $i < count($cpr_arr); $i++) { | |
$sum += $cpr_arr[$i] * $mul_arr[$i]; | |
} | |
if ($sum % 11 == 0) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment