【问题标题】:PHP : How can I Highlight searched words in results and keep the words original text case?PHP:如何在结果中突出显示搜索的单词并保留单词的原始文本大小写?
【发布时间】:2015-12-03 20:40:18
【问题描述】:

我在网站上显示搜索结果,用户可以在其中搜索特定关键字、词。

在结果页面上,我试图在结果中突出显示搜索的单词。 所以用户可以知道哪些词在哪里匹配。

例如

if user searches for : mango

the resulting item original : This Post contains Mango.

the resulting output I want of highlighted item : This Post contains <strong>Mango</strong>

我就是这样用的。

<?php

//highlight all words 
function highlight_words( $title, $searched_words_array) {
    // loop through searched_words_array
    foreach( $searched_words_array as $searched_word ) {
        $title = highlight_word( $title, $searched_word); // highlight word
    }

    return $title; // return highlighted data
}

//highlight single word with color
function highlight_word( $title, $searched_word) {
    $replace = '<strong>' . $searched_word . '</strong>'; // create replacement
    $title = str_ireplace( $searched_word, $replace, $title ); // replace content
    return $title; // return highlighted data
}

我正在从 Sphinx Search Engine 获取搜索词,问题是 Sphinx 以小写形式返回输入/macthed 词。

所以通过使用上面的代码,我的

results becomes : This Post contains <strong>mango</strong>

*注意 mango 中的 m 是小写的。

所以我的问题是如何突出显示单词,即将&lt;strong&gt;&lt;/strong&gt; 包裹在与搜索单词匹配的单词周围? 不丢失它的文本框?

*pp。它与how to highlight search results 的问题不同,我问我的关键字数组是小写的,并使用上述方法将原始单词替换为小写单词。 那么我该如何阻止呢? 其他问题链接也会面临这个问题,因为搜索到的关键字都是小写的。并使用str_ireplace 将匹配它并用小写单词替换它。


更新:

我结合了各种代码 sn-ps 来得到我期望代码做的事情。, 现在它工作得很好。

function strong_words( $title, $searched_words_array) {
    //for all words in array
    foreach ($searched_words_array as $word){

        $lastPos = 0;
        $positions = array();
        //find all positions of word
        while (($lastPos = stripos($title, $word, $lastPos))!== false) {
           $positions[] = $lastPos;
           $lastPos = $lastPos + strlen($word);
        }
        //reverse sort numeric array 
        rsort($positions);

        // highlight all occurances
        foreach ($positions as $pos) {
            $title = strong_word($title , $word, $pos);
        }
    }

//apply strong html code to occurances  
$title = str_replace('#####','</strong>',$title);
$title = str_replace('*****','<strong>',$title);
return $title; // return highlighted data
}


function strong_word($title , $word, $pos){
//ugly hack to not use <strong> , </strong> here directly, as it can get replaced if searched word contains charcters from strong
$title = substr_replace($title, '#####', $pos+strlen($word) , 0) ;
$title = substr_replace($title, '*****', $pos , 0) ;
return $title;
} 

$title = 'This is Great Mango00lk mango';
$words = array('man','a' , 'go','is','g', 'strong') ;

echo strong_words($title,$words);

【问题讨论】:

  • @bub 的主要功能是从那个链接修改的,因为我只是想使用单色,它不是同一个问题。

标签: php str-replace highlighting


【解决方案1】:

function highlight_word( $content, $word, $color ) {
    $replace = '<span style="background-color: ' . $color . ';">' . $word . '</span>'; // create replacement
    $content = str_replace( $word, $replace, $content ); // replace content

    return $content; // return highlighted data
}

function highlight_words( $content, $words, $colors ) {
    $color_index = 0; // index of color (assuming it's an array)

    // loop through words
    foreach( $words as $word ) {
        $content = highlight_word( $content, $word, $colors[$color_index] ); // highlight word
        $color_index = ( $color_index + 1 ) % count( $colors ); // get next color index
    }

    return $content; // return highlighted data
}



// words to find
$words = array(
    'normal',
    'text'
);

// colors to use
$colors = array(
    '#88ccff',
    '#cc88ff'
);

// faking your results_text
$results_text = array(
    array(
        'ab'    => 'AB #1',
        'cd'    => 'Some normal text with normal words isn\'t abnormal at all'
    ), array(
        'ab'    => 'AB #2',
        'cd'    => 'This is another text containing very normal content'
    )
);

// loop through results (assuming $output1 is true)
foreach( $results_text as $result ) {
    $result['cd'] = highlight_words( $result['cd'], $words, $colors );

    echo '<fieldset><p>ab: ' . $result['ab'] . '<br />cd: ' . $result['cd'] . '</p></fieldset>';
}

Original link check here

【讨论】:

    【解决方案2】:

    正则表达式解决方案:

    function highlight_word( $title, $searched_word) {
        return preg_replace('#('.$searched_word.')#i','<strong>\1<strong>',$title) ;
    }
    

    请注意可能在 $searched_word 中被解释为元字符的特殊字符

    【讨论】:

    • 感谢您的回复,我正在寻找一些不是正则表达式的东西,因为它的性能很慢。
    【解决方案3】:

    这是我不久前写的一个代码 sn-p,它可以完全按照您的意愿工作:

    if(stripos($result->question, $word) !== FALSE){
        $word_to_highlight = substr($result->question, stripos($result->question, $word), strlen($word));
        $result->question = str_replace($word_to_highlight, '<span class="search-term">'.$word_to_highlight.'</span>', $result->question);
    }
    

    【讨论】:

    • 谢谢,这是有效的,但对于单次出现的针,即第一次出现。
    【解决方案4】:
    //will find all occurances of all words and make them strong in html
    function strong_words( $title, $searched_words_array) {
        //for all words in array
        foreach ($searched_words_array as $word){
    
            $lastPos = 0;
            $positions = array();
            //find all positions of word
            while (($lastPos = stripos($title, $word, $lastPos))!== false) {
               $positions[] = $lastPos;
               $lastPos = $lastPos + strlen($word);
            }
            //reverse sort numeric array 
            rsort($positions);
    
            // highlight all occurances
            foreach ($positions as $pos) {
                $title = strong_word($title , $word, $pos);
            }
        }
    
    //apply strong html code to occurances  
    $title = str_replace('#####','</strong>',$title);
    $title = str_replace('*****','<strong>',$title);
    return $title; // return highlighted data
    }
    
    
    function strong_word($title , $word, $pos){
    //ugly hack to not use <strong> , </strong> here directly, as it can get replaced if searched word contains charcters from strong
    $title = substr_replace($title, '#####', $pos+strlen($word) , 0) ;
    $title = substr_replace($title, '*****', $pos , 0) ;
    return $title;
    } 
    
    
    $title = 'This is Great Mango00lk mango';
    $word = array('man','a' , 'go','is','g', 'strong') ;
    
    echo strong_words($title,$word);
    

    此代码将查找所有单词的所有出现,并在保持原始文本大小写的同时使它们在 html 中变得强大。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 2012-05-04
      • 2011-01-26
      • 2012-04-01
      • 1970-01-01
      相关资源
      最近更新 更多