Skip to content

Instantly share code, notes, and snippets.

@coquer
Last active March 14, 2020 11:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coquer/1179d7ff5c0b4bd04c8a to your computer and use it in GitHub Desktop.
Save coquer/1179d7ff5c0b4bd04c8a to your computer and use it in GitHub Desktop.
Validate danish CPR using PHP
/**
* Internally validate CPR numbers
* CPR nummer skal udfyldes i rækkefølgen dag-måned-år. Er du f.eks.
* født 2. januar 1965, skal du skrive: 020165. Derudover skal du indtaste
* de sidste 4 cifre i dit cpr. nr. Du skal være fyldt 18 år for at oprette dig som kunde.
* Kontroller venligst oplysningerne en ekstra gang før du fortsætter til næste trin.
* @param $cpr
* @return bool
*/
function validate_cpr_number( $cpr ) {
if ( empty( $cpr ) ) {
return false;
}
$cpr = str_split( $cpr );
$main_int = 0;
$x = 0;
$factors = [ 4, 3, 2, 7, 6, 5, 4, 3, 2, 1 ];
foreach ( $cpr as $cpr_single_number ) {
$main_int += $cpr_single_number * $factors[ $x ];
$x++;
}
return $main_int % 11 == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment