Last active
September 24, 2021 06:35
-
-
Save julienV/fd6b5d75a44c0f13cbf83193047daa15 to your computer and use it in GitHub Desktop.
Validation de numéro de sécurité sociale (NIR) en php
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
<?php | |
/** | |
* Verifie un numero de securite sociale | |
* | |
* @param string $numeroSecu | |
* | |
* @return bool | |
*/ | |
static public function checkNIR(string $numeroSecu): bool | |
{ | |
$numeroSecu = str_replace(" ", "", $numeroSecu); | |
if (strlen($numeroSecu) !== 15) | |
{ | |
return false; | |
} | |
$numeroSansCle = substr($numeroSecu, 0,13); | |
$cle = (int) substr($numeroSecu, 13); | |
// Cas de la corse | |
if (strpos($numeroSansCle, "A")) | |
{ | |
$numeroSansCle = (int) str_replace("A", "0", $numeroSansCle); | |
$numeroSansCle -= 1000000; | |
} | |
elseif (strpos($numeroSansCle, "B")) | |
{ | |
$numeroSansCle = (int) str_replace("B", "0", $numeroSansCle); | |
$numeroSansCle -= 2000000; | |
} | |
$checksum = abs(($numeroSansCle % 97) - 97); | |
return $checksum === $cle; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment