What are the best practices for working with UTF-8 encoding in PHP to avoid issues with regex patterns?

When working with UTF-8 encoding in PHP and using regex patterns, it's important to use the 'u' modifier in your regex pattern to ensure it works correctly with multi-byte characters. This modifier tells PHP to treat the pattern and subject strings as UTF-8 encoded. Additionally, when working with UTF-8 strings, it's recommended to use the mb_ functions in PHP for string manipulation to avoid potential encoding issues.

// Example of using the 'u' modifier with preg_match
$string = "こんにちは";
$pattern = '/^[\p{L}]+$/u'; // Match one or more Unicode letters
if (preg_match($pattern, $string)) {
    echo "String contains only Unicode letters.";
} else {
    echo "String contains non-letter characters.";
}