highlight multiple keywords in search

前端 未结 8 488
庸人自扰
庸人自扰 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:43

    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" ,
                "$1" , $text );
        }
        return $text;
    }
    

    call function

提交回复
热议问题