<?php
class IBAN{
public static $ibanlar = [
'AA' => [12, '^[A-Z0-9]{12}$'],
'AD' => [20, '^[0-9]{4}[0-9]{4}[A-Z0-9]{12}$'],
'AE' => [19, '^[0-9]{3}[0-9]{16}$'],
'AL' => [24, '^[0-9]{8}[A-Z0-9]{16}$'],
'AO' => [21, '^[0-9]{21}$'],
'AT' => [16, '^[0-9]{5}[0-9]{11}$'],
'AX' => [14, '^[0-9]{6}[0-9]{7}[0-9]{1}$'],
'AZ' => [24, '^[A-Z]{4}[A-Z0-9]{20}$'],
'BA' => [16, '^[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}$'],
'BE' => [12, '^[0-9]{3}[0-9]{7}[0-9]{2}$'],
'BF' => [23, '^[0-9]{23}$'],
'BG' => [18, '^[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$'],
'BH' => [18, '^[A-Z]{4}[A-Z0-9]{14}$'],
'BI' => [12, '^[0-9]{12}$'],
'BJ' => [24, '^[A-Z]{1}[0-9]{23}$'],
'BL' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'BR' => [25, '^[0-9]{8}[0-9]{5}[0-9]{10}[A-Z]{1}[A-Z0-9]{1}$'],
'CH' => [17, '^[0-9]{5}[A-Z0-9]{12}$'],
'CI' => [24, '^[A-Z]{1}[0-9]{23}$'],
'CM' => [23, '^[0-9]{23}$'],
'CR' => [17, '^[0-9]{4}[0-9]{13}$'],
'CV' => [21, '^[0-9]{21}$'],
'CY' => [24, '^[0-9]{3}[0-9]{5}[A-Z0-9]{16}$'],
'CZ' => [20, '^[0-9]{4}[0-9]{6}[0-9]{10}$'],
'DE' => [18, '^[0-9]{8}[0-9]{10}$'],
'DK' => [14, '^[0-9]{4}[0-9]{9}[0-9]{1}$'],
'DO' => [24, '^[A-Z0-9]{4}[0-9]{20}$'],
'DZ' => [20, '^[0-9]{20}$'],
'EE' => [16, '^[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}$'],
'ES' => [20, '^[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}$'],
'FI' => [14, '^[0-9]{6}[0-9]{7}[0-9]{1}$'],
'FO' => [14, '^[0-9]{4}[0-9]{9}[0-9]{1}$'],
'FR' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'GB' => [18, '^[A-Z]{4}[0-9]{6}[0-9]{8}$'],
'GE' => [18, '^[A-Z]{2}[0-9]{16}$'],
'GF' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'GI' => [19, '^[A-Z]{4}[A-Z0-9]{15}$'],
'GL' => [14, '^[0-9]{4}[0-9]{9}[0-9]{1}$'],
'GP' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'GR' => [23, '^[0-9]{3}[0-9]{4}[A-Z0-9]{16}$'],
'GT' => [24, '^[A-Z0-9]{4}[A-Z0-9]{20}$'],
'HR' => [17, '^[0-9]{7}[0-9]{10}$'],
'HU' => [24, '^[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}$'],
'IE' => [18, '^[A-Z]{4}[0-9]{6}[0-9]{8}$'],
'IL' => [19, '^[0-9]{3}[0-9]{3}[0-9]{13}$'],
'IR' => [22, '^[0-9]{22}$'],
'IS' => [22, '^[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}$'],
'IT' => [23, '^[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}$'],
'JO' => [26, '^[A-Z]{4}[0-9]{4}[A-Z0-9]{18}$'],
'KW' => [26, '^[A-Z]{4}[A-Z0-9]{22}$'],
'KZ' => [16, '^[0-9]{3}[A-Z0-9]{13}$'],
'LB' => [24, '^[0-9]{4}[A-Z0-9]{20}$'],
'LC' => [28, '^[A-Z]{4}[A-Z0-9]{24}$'],
'LI' => [17, '^[0-9]{5}[A-Z0-9]{12}$'],
'LT' => [16, '^[0-9]{5}[0-9]{11}$'],
'LU' => [16, '^[0-9]{3}[A-Z0-9]{13}$'],
'LV' => [17, '^[A-Z]{4}[A-Z0-9]{13}$'],
'MC' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'MD' => [20, '^[A-Z0-9]{2}[A-Z0-9]{18}$'],
'ME' => [18, '^[0-9]{3}[0-9]{13}[0-9]{2}$'],
'MF' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'MG' => [23, '^[0-9]{23}$'],
'MK' => [15, '^[0-9]{3}[A-Z0-9]{10}[0-9]{2}$'],
'ML' => [24, '^[A-Z]{1}[0-9]{23}$'],
'MQ' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'MR' => [23, '^[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}$'],
'MT' => [27, '^[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$'],
'MU' => [26, '^[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}$'],
'MZ' => [21, '^[0-9]{21}$'],
'NC' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'NL' => [14, '^[A-Z]{4}[0-9]{10}$'],
'NO' => [11, '^[0-9]{4}[0-9]{6}[0-9]{1}$'],
'PF' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'PK' => [20, '^[A-Z]{4}[A-Z0-9]{16}$'],
'PL' => [24, '^[0-9]{8}[0-9]{16}$'],
'PM' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'PS' => [25, '^[A-Z]{4}[A-Z0-9]{21}$'],
'PT' => [21, '^[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}$'],
'QA' => [25, '^[A-Z]{4}[0-9]{4}[A-Z0-9]{17}$'],
'RE' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'RO' => [20, '^[A-Z]{4}[A-Z0-9]{16}$'],
'RS' => [18, '^[0-9]{3}[0-9]{13}[0-9]{2}$'],
'SA' => [20, '^[0-9]{2}[A-Z0-9]{18}$'],
'SC' => [27, '^[A-Z]{4}[0-9]{4}[0-9]{16}[A-Z]{3}$'],
'SE' => [20, '^[0-9]{3}[0-9]{16}[0-9]{1}$'],
'SI' => [15, '^[0-9]{5}[0-9]{8}[0-9]{2}$'],
'SK' => [20, '^[0-9]{4}[0-9]{6}[0-9]{10}$'],
'SM' => [23, '^[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}$'],
'SN' => [24, '^[A-Z]{1}[0-9]{23}$'],
'ST' => [21, '^[0-9]{8}[0-9]{11}[0-9]{2}$'],
'TF' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'TL' => [19, '^[0-9]{3}[0-9]{14}[0-9]{2}$'],
'TN' => [20, '^[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}$'],
'TR' => [22, '^[0-9]{5}[0-9]{1}[A-Z0-9]{16}$'],
'UA' => [25, '^[0-9]{6}[A-Z0-9]{19}$'],
'VG' => [20, '^[A-Z]{4}[0-9]{16}$'],
'WF' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$'],
'XK' => [16, '^[0-9]{4}[0-9]{10}[0-9]{2}$'],
'YT' => [23, '^[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$']
];
private $iban;
public function dogrula($iban){
$this->iban = preg_replace('/[^a-z0-9]+/i', '', trim(strtoupper($iban)));
if(!isset(static::$ibanlar[substr($this->iban,0,2)])){
$hata = 'IBAN ülke kodu geçerli değil veya desteklenmiyor';
}elseif (strlen($this->iban) !== 4+static::$ibanlar[substr($this->iban,0,2)][0]){
$hata = 'IBAN uzunluğu geçersiz';
}elseif(preg_match('/'.static::$ibanlar[substr($this->iban,0,2)][1].'/',substr($this->iban,4)) !== 1){
$hata = 'IBAN biçimi geçersiz';
}elseif(!($this->bcmod((substr($this->iban,4).$this->kodno(substr($this->iban,0,2)).substr($this->iban,2,2)),97)==='1')) {
$hata = 'IBAN sağlama toplamı geçersiz';
} else {
$hata = '';
}
if($hata == ""){
return "Geçerli";
}else{
return $hata;
}
}
public function duzelt(){
return sprintf(
'%s %s %s',
substr($this->iban,0,2) . substr($this->iban,2,2),
substr(substr($this->iban,4,4), 0, 4),
implode(' ', str_split(substr($this->iban,8, static::$ibanlar[substr($this->iban,0,2)][0] -4), 4))
);
}
private function kodno($lR){
$nR = '';
foreach (str_split($lR) as $char) {
$ord = ord($char);
if ($ord >= 65 && $ord <= 90) {
$nR .= (string) ($ord - 55);
} elseif ($ord >= 48 && $ord <= 57) {
$nR .= (string) ($ord - 48);
}
}
return $nR;
}
private function bcmod($x, $y){
if (!function_exists('bcmod')) {
$al = 5;
$mod = '';
do {
$a = (int)$mod . substr($x, 0, $al);
$x = substr($x, $al);
$mod = $a % $y;
} while (strlen($x));
return (string)$mod;
} else {
return bcmod($x, $y);
}
}
}
$i=new IBAN;
echo $i->dogrula("TR320006701000000048830001")."<br>";
echo $i->duzelt()."<br>";
?>
Çıktısı
Geçerli
TR32 0006 7010 0000 0048 8300 01