Skip to content
Advertisement

Split VAT into segments based on Country code

What is the best wat to split any VAT number into segments? First part is the country code, is always 2 alphanumeric. Second is the remaining number.

For example: NL820471616B01 should be split to NL and 820471616B01

BE0824148721 should be split to BE and 0824148721

ATU12345678 should be split to AT and U12345678

I have tried this but that will only suit when there are 2 alphanumeric numbers.

$vat_clean = preg_replace('/[^a-zA-Z0-9]/', '', $_GET['vat']);
$vat_country = substr($vat_clean,2,25);
$vat_code = substr($vat_clean,2);

Advertisement

Answer

Since it appeared the any VAT number starts with 2 alphanumeric numbers I could tweak the existing code to get it fully to work.

$vat_clean = preg_replace('/[^a-zA-Z0-9]/', '', $_GET['vat']);
$vat_country = substr($vat_clean,2,15);
$vat_code = substr($vat_clean,0,2);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement