Last active
August 29, 2022 09:25
-
-
Save andreCatita/5714353 to your computer and use it in GitHub Desktop.
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.
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
<?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