<?php
function checkIBAN(string $iban): bool {
// Normalize input (remove spaces and make uppercase)
$iban = strtoupper(str_replace(' ', '', $iban));
if (!preg_match('/^([A-Z]{2})(\d{2})([A-Z\d]{1,30})$/', $iban, $segments)) {
return false;
}
[, $country, $check, $account] = $segments;
$digits = str_split(strtr($account . $country, array_combine(range('A', 'Z'), range(10, 35))) . '00');
$first = array_shift($digits);
$checksum = array_reduce(
$digits,
function ($carry, $int) {
$carry = ($carry * 10 + (int)$int) % 97;
return $carry;
},
(int)$first
);
return (98 - $checksum) == $check;
}
var_export(checkIBAN('GB29NWBK60161331926819'));
preferences:
44.56 ms | 402 KiB | 5 Q