Home » Php » php – highlight multiple keywords in search

php – highlight multiple keywords in search

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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;
}

Answer:

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);
}

Answer:

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

Answer:

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);
}

Answer:

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); 

Answer:

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/

Answer:

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;
}