What are common pitfalls when validating German postal codes in PHP?

One common pitfall when validating German postal codes in PHP is not accounting for the different formats that can be used. German postal codes can either be five digits or five digits followed by a space and then two letters. To properly validate German postal codes, it is important to check for both formats.

// Validate German postal codes
function validateGermanPostalCode($postalCode) {
    // Check for both formats: 12345 and 12345 AB
    if (!preg_match('/^\d{5}( \w{2})?$/', $postalCode)) {
        return false;
    }
    
    return true;
}

// Example usage
$postalCode = "12345";
if (validateGermanPostalCode($postalCode)) {
    echo "Valid German postal code";
} else {
    echo "Invalid German postal code";
}