How to resolve the algorithm IBAN step by step in the PHP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm IBAN step by step in the PHP programming language

Table of Contents

Problem Statement

The   International Bank Account Number (IBAN)   is an internationally agreed means of identifying bank accounts across national borders with a reduced risk of propagating transcription errors. The IBAN consists of up to 34 alphanumeric characters:

The check digits enable a sanity check of the bank account number to confirm its integrity even before submitting a transaction.

Validate the following fictitious IBAN:   GB82 WEST 1234 5698 7654 32

Details of the algorithm can be found on the Wikipedia page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm IBAN step by step in the PHP programming language

The provided PHP code is designed to validate International Bank Account Numbers (IBANs) using the piece-wise modulo 97 algorithm. An IBAN is a standardized international bank account number format that facilitates the processing of cross-border transactions. Here's a breakdown of the code's functionality:

  1. Input: The code starts by defining an IBAN as a string and removing any spaces from it. It then checks the IBAN's length against the expected length for the given country code. If the length doesn't match, it prompts "IBAN length not valid for [country code]".

  2. Rearranging the IBAN: The first four characters of the IBAN are moved to the end of the string to align with the algorithm's requirements.

  3. Converting Alphabetic Characters to Digits: The code converts any alphabetic characters (A-Z) in the IBAN to their corresponding numerical values (A=10, B=11, ..., Z=35). The resulting string, $iban_all_digits, contains only digits.

  4. Piece-Wise Modulo 97 Calculation: The piece_wise function takes $iban_all_digits as input and applies the piece-wise modulo 97 algorithm. This algorithm divides the string into chunks and iteratively calculates the remainder of each chunk when divided by 97. The final remainder is stored in $remainder.

  5. Validation: The code checks if the $remainder is equal to 1. If it is, the IBAN is considered valid and the code outputs "VALID IBAN!". Otherwise, it outputs "IBAN NOT VALID".

In summary, this code provides a way to validate IBANs by applying the piece-wise modulo 97 algorithm. If the final remainder after applying the algorithm is 1, the IBAN is considered valid.

Source code in the php programming language

<?php 

function piece_wise($iban_all_digits) {
    
    $remainder = NULL;
    $slice = 9;
    
    for ($i=0; $i<strlen($iban_all_digits); $i=$i+$slice)
    {
        if ($i>0)
        {
            $slice = 7;
        } 
        
        $part = $remainder . substr($iban_all_digits, $i, $slice);
        //echo "REMAINDER: " . $remainder . "<br>";
        //echo "PART: $part" . "<br>";
        $remainder = intval($part) % 97;
    }

return $remainder;

}


$iban = "GB82 WEST 1234 5698 7654 32";

//remove space
$iban = str_replace(' ', '', $iban); 

//echo $iban; echo '<br>';
$iban_length = strlen($iban);
$country_code = substr($iban, 0, 2);

/*  
    IBAN lengths are country specific 
    full list available at
    https://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
*/
$lengths = ['GB' => 22];


if ($lengths[$country_code] != $iban_length) 
{ 
    exit ("IBAN length not valid for $country_code");
}


// 2. move first four characters to the end
$iban = substr($iban, 4) . substr($iban, 0, 4);


//3. Replace letters in IBAN with digits 
//(A=10, B=11 ... Z=35)

$iban_arr = str_split($iban, 1);


$iban_all_digits = '';

foreach ($iban_arr as $key=>$value)
{
    if (ctype_alpha($value)) 
    {
        $value = ord($value) - 55;
    }
    $iban_all_digits = $iban_all_digits . $value;
}


if (piece_wise($iban_all_digits) === 1) 
{
    echo "VALID IBAN!";
}

else 
{
    echo "IBAN NOT VALID";
}


  

You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the Wren programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the C# programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the PL/I programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the C# programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Racket programming language