Skip to content

Instantly share code, notes, and snippets.

@gharabaghi
Created February 2, 2021 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gharabaghi/643d1fce482caf3df7f6c5276d1d7ba7 to your computer and use it in GitHub Desktop.
Save gharabaghi/643d1fce482caf3df7f6c5276d1d7ba7 to your computer and use it in GitHub Desktop.
php verify national code for iran
public static function verifyNatCode($code)
{
if (!is_numeric($code) || strlen($code) > 10)
return false;
$tempInt = intval($code);
if ($tempInt == 0)
return false;
while (strlen($code) < 10) {
$code = '0' . $code;
}
$codeCheckValue = substr($code, 9, 1);
$codeArray = [];
for ($i = 0; $i < 10; $i++) {
array_push($codeArray, ['Number' => substr($code, $i, 1), 'Position' => 10 - $i]);
}
$verificationValue = 0;
for ($i = 0; $i < 9; $i++) {
$verificationValue += intval($codeArray[$i]['Number']) * $codeArray[$i]['Position'];
}
$tValue = $verificationValue % 11;
$result = ($tValue < 2) ? $tValue : 11 - $tValue;
return ($result == $codeCheckValue) ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment