I have applied this code for my search function. It is currently working great. However, if I typed a keyword without accented letter, it won't highlight the word.
Example: $text ="iphone mới"
If i typed in keywords "moi", it won't highlight the word "mới" in $text. I also tried u flag in the pattern as suggesttion from google, but it did not work either. Please help...
Here is my code:
<?php
function highlightWords($text, $words)
{
/*** loop of the array of words ***/
foreach ($words as $word)
{
/*** quote the text for regex ***/
$word = preg_quote($word);
/*** highlight the words ***/
$text = preg_replace("/($word)/ui", '<span class="highlight_word">\1</span>', $text);
}
/*** return the text ***/
return $text;
}
/*** example usage ***/
$text = 'this is my iphone mới';
/*** an array of words to highlight ***/
$words = array('moi');
/*** highlight the words ***/
$highlighttext = highlightWords($string, $words);
?>
<html>
<head>
<title>PHPRO Highlight Search Words</title>
<style type="text/css">
.highlight_word{
background-color: pink;
}
</style>
</head>
<body>
<?php echo $highlighttext; ?>