簡體   English   中英

突出顯示搜索中的多個關鍵字

[英]highlight multiple keywords in search

我正在使用此代碼突出顯示搜索關鍵字:

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

但是,這僅突出顯示一個關鍵字。 如果用戶輸入多個關鍵字,則會縮小搜索范圍,但不會突出顯示任何單詞。 如何突出顯示多個單詞?

正則表達式是必經之路!

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

要以不區分大小寫的方式進行匹配,請在正則表達式中添加“ i”

    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';

注意:對於像“ä”這樣的非英語字母,結果可能會因地區而異。

PHP> 5.3.0,請嘗試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;
}

假設單詞輸入為空格分隔的字符串,則可以使用explode

$words = explode(' ', $term);

盡管如果要確保沒有多個空格,則可能需要先從字符串中刪除它們

$term = preg_replace('/\s+/', ' ', trim($term));
$words = explode(' ', $term);

然后,您必須生成一個替換數組

$highlighted = array();
foreach ( $words as $word ){
    $highlighted[] = "<span class='highlight'>".$word."</span>"
}

然后

str_replace($words, $highlighted, $string);

所以放在一起

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

這是突出顯示僅匹配文本的簡單功能。

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

通話功能

突出顯示搜索中的多個關鍵字,包括變音符號

我使用了以前編寫的正則表達式,並用[A-Za-z0-9_äöüÄÖÜ]替換了\\w 如您所見,我添加了變音符äöüÄÖÜ 我還刪除了\\b因此它將匹配搜索詞的任何外觀。

搜索詞:
蘇洗

文本:
防曬洗發露

結果:
ñ閃亮shamp OO


我使用的代碼:

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

如user187291所建議,只需更改以下代碼即可使文本用黃色背景突出顯示。

 return preg_replace($re, '<SPAN style="BACKGROUND-COLOR: #ffff00"><b>$0</b></SPAN>', $text); 

將您的搜索查詢分解為單詞,然后分別突出顯示每個單詞。

不過,用javascript突出顯示可能會更好。 jQuery的“包含”選擇器可能會幫助避免在您進行替換標記元素時出現問題。

http://api.jquery.com/contains-selector/

其他解決方案在查找突出顯示術語時可能不區分大小寫,但不會保留其原始字符串的大小寫。 因此,搜索“ st”將找到“ ST”,但將其突出顯示為搜索詞“ st”。

我使用以下內容。 它首先形成replace數組,然后將str_replace()與數組參數一起使用-避免了遞歸。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM