Skip to content

Instantly share code, notes, and snippets.

@tamboer
Last active December 31, 2015 22:49
Show Gist options
  • Select an option

  • Save tamboer/8055814 to your computer and use it in GitHub Desktop.

Select an option

Save tamboer/8055814 to your computer and use it in GitHub Desktop.
check VAT number
<?php
class Helper {
/**
* http://www.pfz.nl/wiki/invoer-validatie/#Geldig_BTW.2FVAT_nummer
*
* Look if the given input could be a legal VAT-number.
* Accepts input with or without '.' between the numbers, must contain a County-code
*
* Note: This only checks the syntax, NOT IF THE VAT REALLY EXISTS OR IS ACTIVE!
*
* @author Sebastiaan Stok <[email protected]>
*
* @param string $psVatInput
* @return bool
*/
static function isVAT($psVatInput)
{
$psVatInput = trim($psVatInput);
$psVatInput = str_replace('.', '', $psVatInput);
$aVatMatch = array();
// Common syntax
if (!preg_match('/^([a-z]{2})[ ]*(.+)$/is', $psVatInput, $aVatMatch)) {
return false;
}
$aVatMatch[1] = strtoupper($aVatMatch[1]);
$functionName = 'isVAT_' . $aVatMatch[1];
if (function_exists($functionName)) {
return $functionName($psVatInput);
}
//*************************************************
// BELGIAN VAT NUMBER
// 'BE'+10 digits,
// the first digit following the prefix is always zero ("0")
// – e.g. BE0999999999
//
//*************************************************
$aVatRegexes = array(
'AT' => 'U[0-9]{8}',
'BE' => '0[0-9]{9}',
'BG' => '[0-9]{9,10}',
'CY' => '[0-9]{8}[A-Za-z]',
'CZ' => '[0-9]{8,10}',
'DE' => '[0-9]{9}',
'DK' => '[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[0-9]{2}',
'EE' => '[0-9]{9}',
'EL' => '[0-9]{9}',
'ES' => '([A-Za-z0-9][0-9]{7}[A-Za-z0-9])',
'FI' => '[0-9]{8}',
'FR' => '[A-Za-z0-9]{2} ?[0-9]{9}',
'GB' => '([0-9]{3} ?[0-9]{4} ?[0-9]{2}|[0-9]{3} ?[0-9]{4} ?[0-9]{2} ?[0-9]{3}|GD[0-9]{3}|HA[0-9]{3})',
'HU' => '[0-8]{8}',
'IE' => '[0-9][A-Za-z0-9+*][0-9]{5}[A-Za-z]',
'IT' => '[0-9]{11}',
'LT' => '([0-9]{9}|[0-9]{12})',
'LU' => '[0-9]{8}',
'LV' => '[0-9]{11}',
'MT' => '[0-9]{8}',
'NL' => '[0-9]{9}B[0-9]{2}',
'PL' => '[0-9]{10}',
'PT' => '[0-9]{9}',
'RO' => '[0-9]{2,10}',
'SE' => '[0-9]{12}',
'SI' => '[0-9]{8}',
'SK' => '[0-9]{10}',
);
if (!isset($aVatRegexes[$aVatMatch[1]]) || !preg_match('/^([a-z]{2})[ ]*' . $aVatRegexes[$aVatMatch[1]] . '$/is', $psVatInput)) {
return false;
}
return true;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment