highlight multiple keywords in search

前端 未结 8 487
庸人自扰
庸人自扰 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: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, '$0', $text);
    }
    

提交回复
热议问题