【问题标题】:Validate check digit 11 (mod 11) using PHP使用 PHP 验证校验位 11 (mod 11)
【发布时间】:2017-07-05 11:56:25
【问题描述】:

如何使用 PHP 使用校验位 11 (mod 11) 验证 NHS 号码。

在互联网上进行了长时间的搜索,试图找到一个我可以用来验证校验位 11(mod 11)号码的函数,但我找不到,所以我最终自己开发了一个并在这里分享。我希望有人觉得它有用。

【问题讨论】:

    标签: php modulus check-digit


    【解决方案1】:

    带有cmets的代码:

    ###### 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 2023-02-11
      相关资源
      最近更新 更多