Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Last active September 12, 2023 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesobreira/5f29f0e58249d18ec8234af5806926ee to your computer and use it in GitHub Desktop.
Save jesobreira/5f29f0e58249d18ec8234af5806926ee to your computer and use it in GitHub Desktop.
Digito verificador em PHP e Javascript
/**
* Calculates the Modulo 11 for generating the verification digit of bank slips and bank accounts.
*
* @author Pablo Costa <pablo@users.sourceforge.net>
* @link www.febraban.org.br
*
* @param {string} num Numeric string for which the verification digit is to be calculated.
* @param {number} [base=9] Maximum value of multiplication.
* @param {number} [r=0] Specifies the return type. If 0, returns the verification digit, if 1 returns the remainder.
*
* @returns {number} Returns the verification digit or remainder based on the r parameter.
*
* @note Script developed without any reuse of pre-existing code.
* @note It's assumed that the verification of the format of the input variables is done before executing this script.
*/
function modulo11(num, base = 9, r = 0) {
let sum = 0;
let factor = 2;
// Separation of numbers
for (let i = num.length; i > 0; i--) {
// Get each number individually
let currentNum = parseInt(num.charAt(i - 1));
// Multiply the number by the factor
let partial = currentNum * factor;
// Sum of the digits
sum += partial;
if (factor === base) {
// Reset multiplication factor to 2
factor = 1;
}
factor++;
}
// Calculation of modulo 11
if (r === 0) {
sum *= 10;
let digit = sum % 11;
if (digit === 10) {
digit = 0;
}
return digit;
} else if (r === 1) {
let remainder = sum % 11;
return remainder;
}
}
<?php
/**
* Calculates the Modulo 11 for generating the verification digit of bank slips and bank accounts.
*
* @author Pablo Costa <pablo@users.sourceforge.net>
* @link www.febraban.org.br
*
* @param string $num Numeric string for which the verification digit is to be calculated.
* @param int $base Maximum value of multiplication. Defaults to 9.
* @param int $r Specifies the return type. If 0, returns the verification digit, if 1 returns the remainder. Defaults to 0.
*
* @return int Returns the verification digit or remainder based on the $r parameter.
*
* @note Script developed without any reuse of pre-existing code.
* @note It's assumed that the verification of the format of the input variables is done before executing this script.
*/
function modulo_11($num, $base = 9, $r = 0) {
$sum = 0;
$factor = 2;
// Separation of numbers
for ($i = strlen($num); $i > 0; $i--) {
// Get each number individually
$numbers[$i] = substr($num, $i - 1, 1);
// Multiply the number by the factor
$partial[$i] = $numbers[$i] * $factor;
// Sum of the digits
$sum += $partial[$i];
if ($factor == $base) {
// Reset multiplication factor to 2
$factor = 1;
}
$factor++;
}
// Calculation of modulo 11
if ($r == 0) {
$sum *= 10;
$digit = $sum % 11;
if ($digit == 10) {
$digit = 0;
}
return $digit;
} elseif ($r == 1) {
$remainder = $sum % 11;
return $remainder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment