Skip to content

Instantly share code, notes, and snippets.

@webkader
Last active August 29, 2015 14:24
Show Gist options
  • Save webkader/a53d7ab223a201496883 to your computer and use it in GitHub Desktop.
Save webkader/a53d7ab223a201496883 to your computer and use it in GitHub Desktop.
Check if a string is a valid tax number
function isValidTax($vatid)
{
$vatid = str_replace(array(
' ',
'.',
'-',
',',
', '), '', trim($vatid));
$prefix = substr($vatid, 0, 2);
$number = substr($vatid, 2);
$iso_code_2_data = array(
'AT' => 'AT', //Austria
'BE' => 'BE', //Belgium
'BG' => 'BG', //Bulgaria
'DK' => 'DK', //Denmark
'FI' => 'FI', //Finland
'FR' => 'FR', //France
'FX' => 'FR', //France métropolitaine
'DE' => 'DE', //Germany
'GR' => 'EL', //Greece
'IE' => 'IE', //Irland
'IT' => 'IT', //Italy
'LU' => 'LU', //Luxembourg
'NL' => 'NL', //Netherlands
'PT' => 'PT', //Portugal
'ES' => 'ES', //Spain
'SE' => 'SE', //Sweden
'GB' => 'GB', //United Kingdom
'CY' => 'CY', //Cyprus
'EE' => 'EE', //Estonia
'HU' => 'HU', //Hungary
'LV' => 'LV', //Latvia
'LT' => 'LT', //Lithuania
'MT' => 'MT', //Malta
'PL' => 'PL', //Poland
'RO' => 'RO', //Romania
'SK' => 'SK', //Slovakia
'CZ' => 'CZ', //Czech Republic
'SI' => 'SI' //Slovania
);
if (array_key_exists($prefix, $iso_code_2_data) && !empty($number)) {
$response = file_get_contents('http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=' . $iso_code_2_data[$prefix] . '&iso=' . $iso_code_2_data[$prefix] . '&vat=' . $number);
if ($response) {
if (preg_match('/\bvalid VAT number\b/i', $response)) {
return 'valid';
}
if (preg_match('/\binvalid VAT number\b/i', $response)) {
return 'invalid';
}
} else {
return 'unknown';
}
} else {
return 'unknown';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment