highlight multiple keywords in search

前端 未结 8 437
庸人自扰
庸人自扰 2020-12-01 04:58

i\'m using this code to highlight search keywords:

function highlightWords($string, $word)
 {

        $string = str_replace($word, \"

        
相关标签:
8条回答
  • 2020-12-01 05:21

    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); 
    
    0 讨论(0)
  • 2020-12-01 05:22

    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;
    }
    
    0 讨论(0)
  • 2020-12-01 05:35

    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);
    }
    
    0 讨论(0)
  • 2020-12-01 05:38

    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/

    0 讨论(0)
  • 2020-12-01 05:40

    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.

    0 讨论(0)
  • 2020-12-01 05:40

    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;
    }
    
    0 讨论(0)
提交回复
热议问题