You can use Normalizer to normalize the string to Normalization Form KD (NFKD) , where the characters are decomposed, therefore Á (U + 00C1) is decomposed into a combination of the letter A (U + 0041) and the combiner ́ U + 0301):
$str = Normalizer::normalize($str, Normalizer::FORM_KD);
Then you modify the search pattern according to these optional marks:
$pattern = '/('.preg_replace('/\p{L}/u', '$0\p{Mn}?', preg_quote($term, '/')).')/ui';
Replacement is done using preg_replace :
preg_replace($pattern, '<strong>$0</strong>', htmlspecialchars($str))
So the complete method:
private static function highlightTerm($str, $term) { $str = Normalizer::normalize($str, Normalizer::FORM_KD); $pattern = '/('.preg_replace('/\p{L}/u', '$0\p{Mn}?', preg_quote($term, '/')).')/ui'; return preg_replace($pattern, '<strong>$0</strong>', htmlspecialchars($str)); }
source share