Are there best practices for validating VAT numbers in PHP, such as using regular expressions or existing functions?

Validating VAT numbers in PHP can be done using regular expressions or by using existing functions provided by the European Union's VAT Information Exchange System (VIES). The VIES provides a web service that allows you to validate VAT numbers in real-time. Using this service is the most reliable way to validate VAT numbers in PHP.

<?php
function validateVatNumber($vatNumber) {
    $client = new SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl');
    $params = array(
        'countryCode' => substr($vatNumber, 0, 2),
        'vatNumber' => substr($vatNumber, 2),
    );

    try {
        $response = $client->checkVat($params);
        return $response->valid;
    } catch (SoapFault $e) {
        return false;
    }
}

// Example usage
$vatNumber = 'DE123456789';
if (validateVatNumber($vatNumber)) {
    echo 'Valid VAT number';
} else {
    echo 'Invalid VAT number';
}
?>