What are the best practices for performing word searches in PHP to avoid problems with character encoding?

When performing word searches in PHP, it's important to consider character encoding to avoid issues with special characters or non-ASCII characters. One way to handle this is by using the mb_stripos function in PHP, which is multibyte-safe and can handle different character encodings. This function allows for case-insensitive searches while taking into account the encoding of the strings being compared.

$searchTerm = "café";
$haystack = "I love going to the café for coffee.";

if(mb_stripos($haystack, $searchTerm) !== false){
    echo "The search term was found in the haystack.";
} else {
    echo "The search term was not found in the haystack.";
}