8

i'm using this code to highlight search keywords:

function highlightWords($string, $word)
 {

        $string = str_replace($word, "<span class='highlight'>".$word."</span>", $string);
    /*** return the highlighted string ***/
    return $string;

 }

 ....

  $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);

however, this highlights only one keyword. if the user enters more than one keyword, it will narrow down the search but no word is highlighted. how can i highlight more than one word?

input
  • 7,503
  • 25
  • 93
  • 150
  • 2
    I switched to JavaScript for search highlighting. The problem is, that searched values in attributes (or a search, e.g., for `
    – Boldewyn May 03 '10 at 11:05

8 Answers8

28

regular expressions is the way to go!

function highlight($text, $words) {
    preg_match_all('~\w+~', $words, $m);
    if(!$m)
        return $text;
    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~';
    return preg_replace($re, '<b>$0</b>', $text);
}

$text = '
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
';

$words = 'ipsum labore';

print highlight($text, $words);

To match in a case-insensitive manner, add 'i' to the regular expression

    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';

NB: for non-enlish letters like "ä" the results may vary depending on the locale.

user187291
  • 53,363
  • 19
  • 95
  • 127
  • 1
    how do i make it case in-sensitive? if the user types in 'good', it will only show 'good' and not 'Good'. – input May 03 '10 at 11:56
  • 2
    thanks. last question: if the user types in 'good', it will show all the results highlighting the keyword 'good'. however, it won't highlight 'good' of the word 'goodness' altho it will show up in the result. – input May 03 '10 at 12:13
  • 1
    @user187291 How can I tweak it to work with non-english characters AND to highlight down to 1-character keywords? – Joseph Oct 26 '11 at 21:56
  • 3
    But how can you highlight both `mä`and `ma` words? How do you make it be utf-8 independent? – Pathros Oct 22 '15 at 17:24
  • For people that are wondering about the tilde character ~. It's just used as the delimiter for the expressions instead of the forward slash /. – Simon Sep 11 '19 at 12:53
17

PHP > 5.3.0, try preg_filter()

/**
 * Highlighting matching string
 * @param   string  $text           subject
 * @param   string  $words          search string
 * @return  string  highlighted text
 */
public function highlight($text, $words) {
    $highlighted = preg_filter('/' . preg_quote($words, '/') . '/i', '<b><span class="search-highlight">$0</span></b>', $text);
    if (!empty($highlighted)) {
        $text = $highlighted;
    }
    return $text;
}
nibra
  • 3,958
  • 2
  • 20
  • 34
goldsky
  • 801
  • 7
  • 11
  • i try this much better than others, :D – haidarvm Jun 03 '15 at 16:48
  • This one is better than the accepted answer, code is short and works good. – Terry Lin Sep 26 '15 at 12:33
  • 1
    Great! How would you allow to search for both letters like this one: `e` and `é`? How do I specify to use UTF-8 so both letters are considered to be highlighted? – Pathros Oct 22 '15 at 17:12
  • try to add `mb_internal_encoding('UTF-8');` before the preg_*, and add `u` modifier: `preg_filter('/' . preg_quote($words) . '/iu', ... ` – goldsky Oct 25 '15 at 09:30
  • This answer is better than accepted answer for real, and now still work like a charm – Viet Nguyen Apr 18 '19 at 02:20
  • This doesn't highlight only whole words. So if you have a text `lorem ipsum`, and search for `lor`, it would match and highlight `lor`. Also, it would also match and highlight if you search `lorem ips` from `lorem ipsum dolor`. The accepted answer does not have both of these problems as it matches only whole words. So searching for `lorem ips` would only match and highlight `lorem`. – Amir Asyraf Aug 30 '19 at 11:28
6

Assuming the words are entered as a space seperated string you can just use explode

$words = explode(' ', $term);

Although if you want to ensure there are not multiple spaces, you may want to remove them from the string first

$term = preg_replace('/\s+/', ' ', trim($term));
$words = explode(' ', $term);

You do then have to generate a replacement array

$highlighted = array();
foreach ( $words as $word ){
    $highlighted[] = "<span class='highlight'>".$word."</span>"
}

Then

str_replace($words, $highlighted, $string);

So putting it togther

function highlightWords($string, $term){
    $term = preg_replace('/\s+/', ' ', trim($term));
    $words = explode(' ', $term);

    $highlighted = array();
    foreach ( $words as $word ){
        $highlighted[] = "<span class='highlight'>".$word."</span>"
    }

    return str_replace($words, $highlighted, $string);
}
Yacoby
  • 54,544
  • 15
  • 116
  • 120
5

Highlight multiple keywords in search including umlauts

I've used the regex written before and replaced \w with [A-Za-z0-9_äöüÄÖÜ]. As you see I added the umlauts äöüÄÖÜ. I also have removed the \b so it will match any appearance of the search term.

Example

search term:
Su shamp

text:
Sun shiny shampoo

result:
Sun shiny shampoo


The code I've used:

private function getSearchTermToBold($text, $words)
{
    preg_match_all('~[A-Za-z0-9_äöüÄÖÜ]+~', $words, $m);
    if (!$m)
        return $text;
    $re = '~(' . implode('|', $m[0]) . ')~i';
    return preg_replace($re, '<b>$0</b>', $text);
}
mathielo
  • 6,725
  • 7
  • 50
  • 63
Steph Fuchs
  • 51
  • 1
  • 1
4

here is simple function to highlight only match text.

function highlighter_text($text, $words)
{
    $split_words = explode( " " , $words );
    foreach($split_words as $word)
    {
        $color = "#e5e5e5";
        $text = preg_replace("|($word)|Ui" ,
            "<span style=\"background:".$color.";\"><b>$1</b></span>" , $text );
    }
    return $text;
}

call function

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
asi_x
  • 69
  • 5
1

as suggested by user187291, just change following code in order to get text highlighted with yellow background.

 return preg_replace($re, '<SPAN style="BACKGROUND-COLOR: #ffff00"><b>$0</b></SPAN>', $text); 
Fou
  • 896
  • 2
  • 8
  • 19
0

Splits your search query up into words, then highlight each words separately.

It might work out better to perform the highlighting in javascript though. jQuery's "contains" selector will probably help avoid problems of replacing markup elements as you go...

http://api.jquery.com/contains-selector/

Rik Heywood
  • 13,816
  • 9
  • 61
  • 81
0

The other solutions may be case-insensitive in finding the highlight terms, but do not preserve their case of the original string. So searching for "st" will find "ST" but highlight it as "st", the search term.

I use the following. It first forms the replace array, and then uses str_replace() with array parameters - which avoids recursion.

function highlightStr($haystack, $needle, $highlightStyle) {

    if (strlen($highlightStyle) < 1 || strlen($haystack) < 1 || strlen($needle) < 1) {
       return $haystack;
    }

    preg_match_all("/$needle+/i", $haystack, $matches);

    $matches[0] = array_unique($matches[0]);

    if (is_array($matches[0]) && count($matches[0]) >= 1) {
        foreach ($matches[0] as $ii=>$match)
            $replace[$ii]="<span style='$highlightStyle'>$match</span>";

        $haystack = str_replace($matches[0], $replace, $haystack);
    }

    return $haystack;
}
RockwoodON
  • 149
  • 3