Skip to content

Instantly share code, notes, and snippets.

@flowtwo
Created March 21, 2017 10:05
Show Gist options
  • Save flowtwo/b5a99d62da22783b0d71360452d689d7 to your computer and use it in GitHub Desktop.
Save flowtwo/b5a99d62da22783b0d71360452d689d7 to your computer and use it in GitHub Desktop.
Validate Danish CPR
// 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