IBAN Validator

Published on 17.03.2014, by Lubos Dzurik

IBAN Validator

IBAN validator class. Enter valid IBAN number to see some extracted data:

Example - valid IBANs: SK 52 8130 0000 0020 0026 0100 or SK1611110000001000229002

More IBAN examples +

<?php
/**
* $Id$
* Simple IBAN validator class
* Based on partially on http://stackoverflow.com/questions/20983339/validate-iban-php
*/
class IBAN{

    
/**
    * Examined current IBAN code, e.g. SK8309000000000321654777
    */
    
protected $iban;

    
/**
    * TRUE if supplied IBAN is valid
    */
    
protected $valid;

    
/**
    * Defined list of expected IBAN string length for each EU country (without whitespaces)
    */
    
protected $countries = array(
        
'al'=>28,'ad'=>24,'at'=>20,'az'=>28,'bh'=>22,'be'=>26,
        
'ba'=>20,'br'=>29,'bg'=>22,'cr'=>21,'hr'=>21,'cy'=>28,
        
'cz'=>24,'dk'=>18,'do'=>28,'ee'=>20,'fo'=>18,'fi'=>18,
        
'fr'=>27,'ge'=>22,'de'=>22,'gi'=>23,'gr'=>27,'gl'=>18,
        
'gt'=>28,'hu'=>28,'is'=>26,'ie'=>22,'il'=>23,'it'=>27,
        
'jo'=>30,'kz'=>20,'kw'=>30,'lv'=>21,'lb'=>28,'li'=>21,
        
'lt'=>20,'lu'=>20,'mk'=>19,'mt'=>31,'mr'=>27,'mu'=>30,
        
'mc'=>27,'md'=>24,'me'=>22,'nl'=>18,'no'=>15,'pk'=>24,
        
'ps'=>29,'pl'=>28,'pt'=>25,'qa'=>29,'ro'=>24,'sm'=>27,
        
'sa'=>24,'rs'=>22,'sk'=>24,'si'=>19,'es'=>24,'se'=>24,
        
'ch'=>21,'tn'=>24,'tr'=>26,'ae'=>23,'gb'=>22,'vg'=>24
    
);

    
/**
    * Mapping of characters to numbers included inside IBAN string - for modulo 97
    */
    
protected $chars = array(
        
'a'=>10,'b'=>11,'c'=>12,'d'=>13,'e'=>14,'f'=>15,'g'=>16,
        
'h'=>17,'i'=>18,'j'=>19,'k'=>20,'l'=>21,'m'=>22,'n'=>23,
        
'o'=>24,'p'=>25,'q'=>26,'r'=>27,'s'=>28,'t'=>29,'u'=>30,
        
'v'=>31,'w'=>32,'x'=>33,'y'=>34,'z'=>35
    
);

    
/**
    * Swift codes mapped to bank numeric SK codes
    */
    
protected $swift = array(
        
// Note: replace with your own country-specific bank codes
        
'0200' => 'SUBASKBX','0900' => 'GIBASKBX','0720' => 'NBSBSKBX',
        
'1100' => 'TATRSKBX','1111' => 'UNCRSKBX','3000' => 'SLZBSKBA',
        
'3100' => 'LUBASKBX','5200' => 'OTPVSKBX','5600' => 'KOMASK2X',
        
'5900' => 'PRVASKBA','6500' => 'POBNSKBA','7300' => 'INGBSKBX',
        
'7500' => 'CEKOSKBX','7930' => 'WUSTSKBA','8050' => 'COBASKBX',
        
'8100' => 'KOMBSKBA','8120' => 'BSLOSK22','8130' => 'CITISKBA',
        
'8150' => 'ABNASKBX','8170' => 'KBSPSKBX','8160' => 'EXSKSKBX',
        
'8180' => 'SPSRSKBA','8320' => 'JTBPSKBA','8330' => 'FIOZSKBA',
        
'8360' => 'BREXSKBX','8370' => 'OBKLSKBA','8410' => 'RIDBSKBX',
        
'8420' => 'BFKKSKBB','8430' => 'KODBSKBX','9951' => 'XBRASKB1',
        
'9952' => 'TPAYSKBX',
    );

    
/**
    * IBAN string, e.g. SK16 1111 0000 0010 0022 9002
    * @param string $iban
    */
    
public function __construct($iban){
        
$this->iban $iban;
    }

    
/**
    * Return TRUE is IBAN is valid, otherwise FALSE
    * @param string $iban e.g. SK31 1200 0000 1987 4263 7541 or SK5281300000002000260100
    */
    
public function isValid(){
        if(
$this->valid === null){
            
$this->valid false;
            
$this->iban strtolower(str_replace(' ','',$this->iban));
            
$code substr($this->iban,0,2);
            if(
array_key_exists($code$this->countries)
                && 
strlen($this->iban) == $this->countries[$code])
            {
                
$movedChar substr($this->iban4).substr($this->iban,0,4);
                
$movedCharArray str_split($movedChar);
                
$newString "";
                foreach(
$movedCharArray as $key => $value){
                    if(!
is_numeric($movedCharArray[$key])){
                        
$movedCharArray[$key] = $this->chars[$movedCharArray[$key]];
                    }
                    
$newString .= $movedCharArray[$key];
                }
                
$this->valid bcmod($newString'97') == 1;
            }
        }
        return 
$this->valid;
    }

    
/**
    * Return SWIFT (BIC) bank code from current IBAN
    * e.g. SK5281300000002000260100 returns CITISKBA
    */
    
public function getSwift(){
        if(!empty(
$this->iban) && $this->isValid()){
            
$code substr($this->iban44); // e.g. 1100
            
if(array_key_exists($code$this->swift)){
                return 
$this->swift[$code];
            }
        }
    }

    
/**
    * Extract account number from IBAN
    * (all characters from 8th position to the right)
    */
    
public function getAccount(){
        if(!empty(
$this->iban) && $this->isValid()){
            return 
substr($this->iban8);
        }
    }

    
/**
    * Extract bank code from IBAN
    * (characters 4 - 8)
    */
    
public function getBankNum(){
        if(!empty(
$this->iban) && $this->isValid()){
            return 
substr($this->iban44);
        }
    }

    
/**
    * Extract country code from IBAN
    * (first 2 characters)
    */
    
public function getCountryCode(){
        if(!empty(
$this->iban) && $this->isValid()){
            return 
strtoupper(substr($this->iban02));
        }
    }

}


Leave your comment..
Email will be converted into something like [michael AT gmail DOT com]
Note: Offensive and unrelated comments will be deleted.
Please enter result from the picture above.