highlight multiple keywords in search

2019-01-08 14:39发布

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?

8条回答
劳资没心,怎么记你
2楼-- · 2019-01-08 14:48

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/

查看更多
Fickle 薄情
3楼-- · 2019-01-08 14:49

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);
}
查看更多
成全新的幸福
4楼-- · 2019-01-08 14:49

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); 
查看更多
唯我独甜
5楼-- · 2019-01-08 14:50

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);
}
查看更多
趁早两清
6楼-- · 2019-01-08 14:52

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

查看更多
走好不送
7楼-- · 2019-01-08 14:57

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;
}
查看更多
登录 后发表回答