1

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; ?>
Giang Nguyen
  • 495
  • 8
  • 22
  • Your problem is unclear. – anubhava Jan 30 '15 at 17:15
  • 1
    possible duplicate of [PHP-REGEX: accented letters matches non-accented ones, and vice versa. How to achieve this?](http://stackoverflow.com/questions/10837247/php-regex-accented-letters-matches-non-accented-ones-and-vice-versa-how-to-ac) – Patrick Q Jan 30 '15 at 18:06
  • This function is currently finding matched words of strings in database against typed in keywords from users. If there is matched words, it will highlight it. However, if if there is accented words in database, then this function won't highlight it. Example: there is a word "mới" in database and user typed in keywords: "moi", it won't work. – Giang Nguyen Jan 30 '15 at 18:10
  • The solution depends on how the search string should be treated. From your example, `moi` can match `mới` or `mọi`. But how about `mơi`? You probably want it to match `mời` and `mới`, but can it match `moi`? Same for `mòi`, do you want it to match `mời` and `moi`? Depending on the desired behavior, the solution can be simple or complex. – nhahtdh Jan 31 '15 at 08:01

1 Answers1

0

You could use this code:

setlocale(LC_ALL, 'en_US');
$word = iconv("utf-8","ascii//TRANSLIT",$word);
/*** quote the text for regex ***/

This will remove all the diacritics from the $word.

You can then iterate through each character and replace it with a character class (if the character has alternative forms). For example o becomes [ớọơờớòo].

Note: nhahtdh makes a good point. I don't know what language these diacritics come from, much less if any characters should become characters with different diacritics.

If you want to do something along that line, don't flatten the $word and add rules for accented characters, like: ơ becomes [ờớơ].

Laurel
  • 5,965
  • 14
  • 31
  • 57