Skip to content

Instantly share code, notes, and snippets.

@hewerthomn
Created April 28, 2017 12:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hewerthomn/cc60a9db2d96ef0ffaecd951821fe410 to your computer and use it in GitHub Desktop.
Save hewerthomn/cc60a9db2d96ef0ffaecd951821fe410 to your computer and use it in GitHub Desktop.
<?php
/**
* Calculate the mod11
* @param string $baseVal
* @param string $separator
* @return string
*/
public function mod11($baseVal = "", $separator = '-')
{
$result = "";
$weight = [ 2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7 ];
/* For convenience, reverse the string and work left to right. */
$reversedBseVal = strrev($baseVal);
for ($i=0, $sum=0; $i < strlen($reversedBseVal); $i++) {
/* Calculate product and accumulate. */
$sum += substr($reversedBseVal, $i, 1) * $weight[$i];
}
/* Determine check digit, and concatenate to base value. */
$remainder = $sum % 11;
switch ($remainder) {
case 0:
case 1:
$result = "{$baseVal}{$separator}0";
break;
default:
$checkDigit = 11 - $remainder;
$result = "{$baseVal}{$separator}{$checkDigit}";
break;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment