Skip to content

Instantly share code, notes, and snippets.

@julienV
Last active September 24, 2021 06:35
Show Gist options
  • Save julienV/fd6b5d75a44c0f13cbf83193047daa15 to your computer and use it in GitHub Desktop.
Save julienV/fd6b5d75a44c0f13cbf83193047daa15 to your computer and use it in GitHub Desktop.
Validation de numéro de sécurité sociale (NIR) en php
<?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