Last active
July 13, 2018 08:11
-
-
Save spicydog/4dc89ca4f2e7acfb3133d45f125bdf68 to your computer and use it in GitHub Desktop.
The PHP function to check Thai national ID
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
/** | |
* 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