How can PHP be used to handle non-ASCII characters in user input validation?

When handling non-ASCII characters in user input validation with PHP, it is important to ensure that the input is properly sanitized and validated to prevent any potential security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to handle non-ASCII characters is to use the `mb_strlen()` function to check the length of the input string, as it supports multi-byte character encoding. Additionally, you can use regular expressions with the `preg_match()` function to validate the input for specific characters or patterns.

// Validate user input for non-ASCII characters
$user_input = $_POST['user_input'];

// Check if input contains any non-ASCII characters
if (mb_strlen($user_input, 'UTF-8') != strlen($user_input)) {
    // Handle error for non-ASCII characters
    echo "Error: Input contains non-ASCII characters";
} else {
    // Proceed with further validation or processing
    // Additional validation code here
}