I've been trying to bold a search term in a sentence. If the sentence is Engliš is spoken wörldwide. If my search term is spoken world I want to get Engliš is <b>spoken wörld</b>wide.
I've used this function:
function highlightWords($text, $searchTerm){
$corr = ['a' => '[aäâ]', 'o' => '[oöòóôõ]', 'c' => '[cç]', 's' => '[şśšșŝ]', 'y' => '[ýÿŷȳy]', 'o' => '[ôöòóøōoõ]', 'n' => '[ñńňn]', 'u' => '[üu]'];
$key = preg_quote($searchTerm);
$pattern = '/' . strtr($key, $corr) . '/iu';
$text = preg_replace($pattern, '<b>$0</b>', $text);
return $text;
}
It supposed to work, but I get really strange behavior. Few examples are:
Text is Sygmaý çykdy deşdi-sähra düzünden (sorry for the weird sentence). When $searchTerm is duz it perfectly works, I get Sygmaý çykdy deşdi-sähra <b>düz</b>ünden. If I change search term to sahra, the function returns just plain Sygmaý çykdy deşdi-sähra düzünden.
Works with cykdy and çykdy.
But doesn't work with neither sygmay nor sygmaý. But works with Sygmaý with capital letter.
What should I need to fix in order to get highlighted search term in all scenarios?