Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active July 13, 2018 08:11
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 spicydog/4dc89ca4f2e7acfb3133d45f125bdf68 to your computer and use it in GitHub Desktop.
Save spicydog/4dc89ca4f2e7acfb3133d45f125bdf68 to your computer and use it in GitHub Desktop.
The PHP function to check Thai national ID
/**
* Validate Thai national ID
* @param string $nationalId
* @link https://th.wikipedia.org/wiki/เลขประจำตัวประชาชนไทย
* @return bool
*/
public function isValidNationalId(string $nationalId)
{
if (strlen($nationalId) === 13) {
$digits = str_split($nationalId);
$lastDigit = array_pop($digits);
$sum = array_sum(array_map(function($d, $k) {
return ($k+2) * $d;
}, array_reverse($digits), array_keys($digits)));
return $lastDigit === strval((11 - $sum % 11) % 10);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment