$re = '/\b[A-Z]{2}\d\d(?:(?: ?[A-Z\d]{4}){2,6} ?[A-Z\d]{3}|( ?[A-Z\d]{4}){3,7}(?: ?[A-Z\d]{1,2})?)\b/mi';
$str = 'NL64ABNC0417164300
NL64 ABNC 0417 164
NL64 ABNC 0417 1643
NL64 ABNC 0417 1643 0
NL64 ABNC 0417 1643 012
NL64 ABNC 0417 1643 0123
NL64 ABNC 0417 1643 0123 0
NL64 ABNC 0417 1643 0123 0123 0123 0123 01
This is a customer iban DE89 3704 0044 0532 0130 00 from somewhere
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php