What are the best practices for handling character encoding issues in PHP when using preg_match?

When dealing with character encoding issues in PHP while using preg_match, it is important to ensure that the regular expression pattern and the input string are in the same character encoding. This can be achieved by using the 'u' modifier in the preg_match function, which tells PHP to treat the pattern and the input string as UTF-8 encoded. Additionally, it is recommended to set the default_charset in php.ini to UTF-8 to avoid any encoding conflicts.

// Set the default character encoding to UTF-8
ini_set('default_charset', 'UTF-8');

// Define the input string and regular expression pattern
$input = "Hello, 你好";
$pattern = '/\p{Han}+/u'; // Match Chinese characters

// Use preg_match with the 'u' modifier to handle UTF-8 encoding
if (preg_match($pattern, $input)) {
    echo "Chinese characters found in the input string.";
} else {
    echo "No Chinese characters found in the input string.";
}