Newsgroup: it.comp.www.php
Titolo: classe IBAN

Ciao avrei bisogno di una classe per verificare se un codice IBAN è
valido o meno. Ho cercato su internet e ho trovato diverse classi che
sembrerebbero essere generali ovvero anche per IBAN italiani. Le ho
provate ma non ce ne una che funzioni (o sono io che non ho capito
come usarle). D' altro canto esistono diversi siti che offrono il
servizio di verifica IBAN  (es: 
http://interno.comze.com/modules.php?name=Banche#_Verifica_IBAN
)
e funzionano perfettamente (quelo dell'esempio è in php). Qualcuno di
voi sa indicarmi uno script che svolga questo compito. Grazie mille.


wrote:

<?php
        // See: 
http://www.europebanks.info/ibanguide.htm
        // 
http://www.ecbs.org/iban.htm
        // 
http://www.tbg5-finance.org/ibandocs.shtml/
        function ISO13616($string)
        {
                $isostr = substr($string, 4) . substr($string, 0, 4);
                // Ogni lettera viene convertita in una coppia di numeri
                for ($i = 0; $i < 26; $i++)
                        $isostr = str_replace(array(chr(65+$i)),
array("".$i+10), $isostr);
                return $isostr;
        }
        function largemodulus($string, $number)
        {
                $n      = strlen($string);
                $a      = 1;
                $r      = 0;
                for ($i = 0; $i < $n; $i++)
                {
                        $d = substr($string, $n - $i -1, 1);
                        $r += $d * $a;
                        $a = ($a * 10) % $number;
                }
                return $r % $number;
        }
        function tpl2regexp($tpl)
        {
                $match  = array(
                        'A'     => '\S', 'B'    => '\S', 'C'    => '\D',
                        'F'     => '\d', 'L'    => '\D', 'U'    => '\D',
                        'W'     => '\S',
                );
                preg_match_all('/([ABCFLUW])(\d\d)/', $tpl, $gregs,
PREG_SET_ORDER);
                $regexp = '';
                foreach($gregs as $info)
                {
                        list(,$type,$length) = $info;
                        $regexp .= "($match[$type]{" . (int)$length .
'})';
                }
                return $regexp;
        }
        function ibanana($iban)
        {
                $ibaninfo = array(
                        //      A = EU REGULATION 924/2009
                        //          Length National struct      A  IBAN
example
                        'AL' => array(28, "F08A16",             0,
'AL47212110090000000235698741'),
                        'AD' => array(24, "F04F04A12",          0,
'AD1200012030200359100100'),
                        'AT' => array(20, "F05F11",             1,
'AT611904300234573201'),
                        'BA' => array(20, "F03F03F08F02",       0,
'BA391290079401028494'),
                        'BG' => array(22, "U04F04F02A08",       1,
'BG80BNBG96611020345678'),
                        'BE' => array(16, "F03F07F02",          1,
'BE68539007547034'),
                        'CH' => array(21, "F05A12",             0,
'CH9300762011623852957'),
                        'CY' => array(28, "F03F05A16",          1,
'CY17002001280000001200527600'),
                        'DE' => array(22, "F08F10",             1,
'DE89370400440532013000'),
                        'CZ' => array(24, "F04F06F10",          1,
'CZ6508000000192000145399'),
                        'DK' => array(18, "F04F09F01",          1,
'DK5000400440116243'),
                        'EE' => array(20, "F02F02F11F01",       1,
'EE382200221020145685'),
                        'ES' => array(24, "F04F04F01F01F10",    1,
'ES9121000418450200051332'),
                        'FI' => array(18, "F06F07F01",          1,
'FI2112345600000785'),
                        'FR' => array(27, "F05F05A11F02",       1,
'FR1420041010050500013M02606'),
                        'FO' => array(18, "F04F09F01",          0,
'FO6264600001631634'),
                        'GB' => array(22, "U04F06F08",          1,
'GB29NWBK60161331926819'),
                        'GE' => array(22, "U02F16",             0,
'GE29NB0000000101904917'),
                        'GL' => array(18, "F04F09F01",          0,
'GL8964710001000206'),
                        'GI' => array(23, "U04A15",             1,
'GI75NWBK000000007099453'),
                        'GR' => array(27, "F03F04A16",          1,
'GR1601101250000000012300695'),
                        'HR' => array(21, "F07F10",             0,
'HR1210010051863000160'),
                        'HU' => array(28, "F03F04F01F15F01",    1,
'HU42117730161111101800000000'),
                        'IE' => array(22, "U04F06F08",          1,
'IE29AIBK93115212345678'),
                        'IS' => array(26, "F04F02F06F10",       1,
'IS140159260076545510730339'),
                        'IL' => array(23, "F03F03F13",          0,
'IL620108000000099999999'),
                        'KW' => array(30, "U04F22",             0,
'KW81CBKU0000000000001234560101'),
                        'IT' => array(27, "U01F05F05A12",       1,
'IT60X0542811101000000123456', 'DG,ABI,CAB,CC'),
                        'KZ' => array(20, "F03A13",             0,
'KZ86125KZT5004100100'),
                        'LB' => array(28, "F04A20",             0,
'LB62099900000001001901229114'),
                        'LI' => array(21, "F05A12",             1,
'LI21088100002324013AA'),
                        'LT' => array(20, "F05F11",             1,
'LT121000011101001000'),
                        'LU' => array(20, "F03A13",             1,
'LU280019400644750000'),
                        'LV' => array(21, "U04A13",             1,
'LV80BANK0000435195001'),
                        'ME' => array(22, "F03F13F02",          0,
'ME25505000012345678951'),
                        'MC' => array(27, "F05F05A11F02",       1,
'MC1112739000700011111000h79'),
                        'MK' => array(19, "F03A10F02",          0,
'MK07300000000042425'),
                        'MT' => array(31, "U04F05A18",          1,
'MT84MALT011000012345MTLCAST001S'),
                        'NL' => array(18, "U04F10",             1,
'NL91ABNA0417164300'),
                        'MU' => array(30, "U04F02F02F12F03U03", 0,
'MU17BOMM0101101030300200000MUR'),
                        'NO' => array(15, "F04F06F01",          1,
'NO9386011117947'),
                        'PL' => array(28, "F08n16",             1,
'PL27114020040000300201355387'),
                        'PT' => array(25, "F04F04F11F02",       1,
'PT50000201231234567890154'),
                        'RO' => array(24, "U04A16",             1,
'RO49AAAA1B31007593840000'),
                        'SA' => array(24, "F02A18",             0,
'SA0380000000608010167519'),
                        'RS' => array(22, "F03F13F02",          0,
'RS35260005601001611379'),
                        'SI' => array(19, "F05F08F02",          1,
'SI56191000000123438'),
                        'SE' => array(24, "F03F16F01",          1,
'SE4550000000058398257466'),
                        'TR' => array(26, "F05A01A16",          0,
'TR330006100519786457841326'),
                        'TN' => array(24, "F02F03F13F02",       0,
'TN5914207207100707129648'),
                        'SM' => array(27, "U01F05F05A12",       0,
'SM86U0322509800000000270100'),
                        'SK' => array(24, "F04F06F10",          1,
'SK3112000000198742637541'),
                );
                if ('SELF TEST' == $iban)
                {
                        $passed = 1;
                        foreach($ibaninfo as $country => $info)
                        {
                                $test   = ibanana($info[3]);
                                print "$country " . ($test[0] ? "OK" :
"FAILED: $test[1]" ) . "\n";
                                $passed *= $test[0];
                        }
                        die(($passed ? "All tests passed" : "Self-test
failed") . "\n");
                }
                $prefix = '([A-Z][A-Z])([0-9][0-9])';
                if (preg_match('/[^A-Z0-9]+/', $iban = strtoupper($iban),
$gregs))
                        return array(false, "illegal character(s) in IBAN:
'$gregs[1]'");
                if (!preg_match("/^$prefix/", $start = substr($iban, 0,
4), $gregs))
                        return array(false, "illegal IBAN prefix
'$start'");
                list(,$country,$checkdigit) = $gregs;
                $checkdigit     = (int)$checkdigit;
                if (($checkdigit <= 1) || ($checkdigit > 98))
                        return array(false, 'invalid check digit');
                if (!array_key_exists($country, $ibaninfo))
                        return array(false, "unknown country code
'$country'");
                list($length, $format) = $ciban = $ibaninfo[$country];
                $regexp = tpl2regexp($format);
                if (!preg_match("/^$prefix$regexp/", $iban, $gregs))
                        return array(false, "bad structure for country
'$country'");
                array_shift($gregs);
                if (count($ciban) > 4)
                        $data   = array_combine(explode(',',
"COUNTRY,CHECKDIGIT,$ciban[4]"), $gregs);
                else
                        $data   = array();
                if (1 != largemodulus(ISO13616($iban), 97))
                        return array(false, "bad check digit value");
                return array(true, $data);
        }
        ibanana('SELF TEST');
?>
<?php
	require 'ibanana.php';
	list($okay, $info) = ibanana('IT60X0542811101000000123456');
	if (!$okay)
		die("IBAN errato: $info");
	print_r($info);
?>
Mi da':
Array
(
    [COUNTRY] => IT
    [CHECKDIGIT] => 60
    [DG] => X
    [ABI] => 05428
    [CAB] => 11101
    [CC] => 000000123456
)
S.E.&O., "as is", caveat emptor, insomma cazzitùa :-D
Leonardo che garantisce freschezza, ma non verità
-- 
Enough, enough!  But, stranger, ere we part,
Glancing farewell to each nefarious bier,
This warning would I beg you take to heart:
There is an end to e'en the worst career!