<?php
# this script converts the IBAN_registry.txt file's entries to registry.txt format (php-iban's required internal format).
# init
require_once(dirname(dirname(__FILE__)) . '/php-iban.php');
date_default_timezone_set('UTC'); # mutes a warning
# read registry
$data = `iconv -f utf8 -t ascii --byte-subst="<0x%x>" --unicode-subst="<U+%04X>" 'IBAN_Registry.txt'`;
if($data == '') { die("Couldn't read IBAN_Registry.txt - try downloading from the location described in the REGISTRY-URL file."); }
# print header line
print "country_code|country_name|domestic_example|bban_example|bban_format_swift|bban_format_regex|bban_length|iban_example|iban_format_swift|iban_format_regex|iban_length|bban_bankid_start_offset|bban_bankid_stop_offset|bban_branchid_start_offset|bban_branchid_stop_offset|registry_edition|country_sepa\n";
# break in to lines
$lines = preg_split('/[\r\n]+/',$data);
# display
foreach($lines as $line) {
# if it's not a blank line, and it's not the header row
if($line != '' && !preg_match('/SEPA Country/',$line)) {
# extract individual tab-separated fields
$bits = explode("\t",$line);
# remove quotes and superfluous whitespace on fields that have them.
for($i=0;$i<count($bits);$i++) {
$bits[$i] = preg_replace('/^"(.*)"$/','$1',$bits[$i]);
$bits[$i] = preg_replace('/^ */','',$bits[$i]);
$bits[$i] = preg_replace('/ *$/','',$bits[$i]);
}
# assigned fields to named variables
# print "-------\n";
# print $line;
# print "-------\n";
list($country_name,$country_code,$domestic_example,$bban,$bban_structure,$bban_length,$bban_bi_position,$bban_bi_length,$bban_bi_example,$bban_example,$iban,$iban_structure,$iban_length,$iban_electronic_example,$iban_print_example,$country_sepa,$contact_details) = $bits;
# sanitise
$country_code = strtoupper(substr($country_code,0,2)); # sanitise comments away
$bban_structure = preg_replace('/[:;]/','',$bban_structure); # errors seen in Germany, Hungary entries
$iban_structure = preg_replace('/, .*$/','',$iban_structure); # duplicates for FO, GL seen in DK
$iban_electronic_example = preg_replace('/, .*$/','',$iban_electronic_example); # duplicates for FO, GL seen in DK
if($country_code=='MU') {
$iban_electronic_example = str_replace(' ','',$iban_electronic_example); # MU example has a spurious space
}
if($country_code=='CZ') {
$iban_electronic_example = preg_replace('/ \w{10,}+$/','',$iban_electronic_example); # extra example for CZ
$iban_print_example = preg_replace('/^(CZ.. .... .... .... .... ....).*$/','$1',$iban_print_example); # extra example
}
if($country_code=='FI') {
# remove additional example
$iban_electronic_example = preg_replace('/ or .*$/','',$iban_electronic_example);
# fix bban example to remove verbosity and match domestic example