Today again I’m here because there’s a special Modulus Check Digit of the norm ISO/IEC 7064, the Mod 11,2, that I wasn’t able to find the code for in PHP.
To help you all I decided to build a version of it. Mod 11 2 isn’t a very known modulus because it isn’t used very often and perhaps that’s why there isn’t much information around.
For example the Mod 97,10 is used to calculate the check digit of an IBAN (International Bank Account Number) and so you can find it in many places.
The Mod 11-2 is used in medical related services, that’s where you will find it most of the times.
Code can also be found in the gist here and in the box below.
Share, enjoy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Iso7064mod112 { public $code; public function encode($code) { $this->code = $code; $c = $this->computeCheck($this->code); if ($c == 10) { return $this->code . "X"; } else { return $this->code . $c; } } public function verify($code) { $this->code = $code; return (computeCheck(substr($this->code, 0, -1)) == getCheckDigit($this->code)); } public function computeCheck($str) { $p = 0; for ($i = 0; $i < strlen($str); $i++) { $c = $str[$i]; $p = 2 * ($p + $c); } $p %= 11; return (12 - $p) % 11; } public function getCheckDigit($str) { $c = substr($str, -1); if ($c == 'X' || $c == 'x') { return 10; } else { return $c; } } } |