Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function checkIBAN($iban){
- $patternIBAN = "/^[0-9A-Z]{22}$/";
- if (!preg_match($patternIBAN, $iban)) {
- return false;
- }
- if ($iban[0] != 'B' || $iban[1] != 'G'){
- return false;
- }
- $str = substr($iban, 4) . substr($iban, 0, 4);
- $num = "";
- for ($i = 0; $i < strlen($str); $i++){
- if (is_numeric($str[$i])){
- $num = $num . $str[$i];
- } else {
- $n = ord($str[$i]) - ord('A') + 10; // A=10, B=11, C=12...
- $num = $num . $n;
- }
- }
- if (mod($num, 97) != 1){
- return false;
- }
- $str = substr($iban, 4) . substr($iban, 0, 4);
- $str[20] = $str[21] = '0';
- $num = "";
- for ($i = 0; $i < strlen($str); $i++){
- if (is_numeric($str[$i])){
- $num = $num . $str[$i];
- } else {
- $n = ord($str[$i]) - ord('A') + 10; // A=10, B=11, C=12...
- $num = $num . $n;
- }
- }
- $calculatedControl = 98 - mod($num, 97);
- $realControl = intval($iban[2].$iban[3]);
- if ($calculatedControl != $realControl){
- return false;
- }
- return true;
- }
- function mod($num, $a){
- // Initialize result
- $res = 0;
- // One by one process
- // all digits of 'num'
- for ($i = 0; $i < strlen($num); $i++){
- $res = ($res * 10 + $num[$i] - '0') % $a;
- }
- return $res;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement