使用 PHP 验证校验位 11 (mod 11)
Validate check digit 11 (mod 11) using PHP
如何使用 PHP.
使用校验位 11 (mod 11) 验证 NHS 编号
在互联网上搜索了很长时间,试图找到一个可以用来验证校验位 11 (mod 11) 的函数,但找不到,所以我最终自己开发了一个并分享在这里。我希望有人觉得它有用。
带注释的代码:
###### Check Digit 11 Modulus
function mod11($nhs_no){
//Get Last Number
$checkDigit = (int)substr($nhs_no, -1);
//Get Remaining Numbers
$numbers = substr($nhs_no, 0, -1);
// Sort Numbers Into an Array
$numbers_array = str_split($numbers);
//Define the base for the weights
$base=2;
// Multiply the weights to the numbers
$numbers_array_reversed = array_reverse($numbers_array);
foreach($numbers_array_reversed as $number){$multiplier[$number.'x'.$base]=(float)$number * $base;$base++;}
//Get the total sum
$sum =(float) array_sum($multiplier);
$modulus = 11;
// Divide the sum by check digit 11
$divider = (float)($sum / $modulus);
// Get the remainder
$remainder = (int)(($divider - (int)$divider)*$modulus);
// Check if the remainder is matching with the last check digit
$results = (int) ($modulus-$remainder);
// Return true or false
return ($results == $checkDigit ? true : false);
}
如何使用 PHP.
使用校验位 11 (mod 11) 验证 NHS 编号在互联网上搜索了很长时间,试图找到一个可以用来验证校验位 11 (mod 11) 的函数,但找不到,所以我最终自己开发了一个并分享在这里。我希望有人觉得它有用。
带注释的代码:
###### Check Digit 11 Modulus
function mod11($nhs_no){
//Get Last Number
$checkDigit = (int)substr($nhs_no, -1);
//Get Remaining Numbers
$numbers = substr($nhs_no, 0, -1);
// Sort Numbers Into an Array
$numbers_array = str_split($numbers);
//Define the base for the weights
$base=2;
// Multiply the weights to the numbers
$numbers_array_reversed = array_reverse($numbers_array);
foreach($numbers_array_reversed as $number){$multiplier[$number.'x'.$base]=(float)$number * $base;$base++;}
//Get the total sum
$sum =(float) array_sum($multiplier);
$modulus = 11;
// Divide the sum by check digit 11
$divider = (float)($sum / $modulus);
// Get the remainder
$remainder = (int)(($divider - (int)$divider)*$modulus);
// Check if the remainder is matching with the last check digit
$results = (int) ($modulus-$remainder);
// Return true or false
return ($results == $checkDigit ? true : false);
}