PHP Example of ISO 7064 Mod 11,2 or Modulus 11,2 of the norm ISO/IEC 7064. Is is not very used, that's why I am sharing.
<?php | |
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; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment