Are there best practices for handling special characters like umlauts in PHP form validation?

Special characters like umlauts can cause issues in form validation if not handled properly. To ensure that these characters are accepted and validated correctly, it is recommended to use UTF-8 encoding and sanitize input data to prevent any potential security vulnerabilities.

// Set UTF-8 encoding for PHP
header('Content-Type: text/html; charset=utf-8');

// Sanitize input data with htmlspecialchars to prevent XSS attacks
$clean_input = htmlspecialchars($_POST['input_field'], ENT_QUOTES, 'UTF-8');

// Validate the sanitized input data
if (preg_match('/^[a-zA-ZäöüÄÖÜß\s]+$/', $clean_input)) {
    // Input is valid
} else {
    // Input is invalid
}