PHP:如何在结果中突出显示搜索到的单词并保持单词的原始大小写


PHP : How can I Highlight searched words in results and keep the words original text case?

我在网站上显示搜索结果,用户可以在这里搜索特定的关键词。

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

例如

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搜索引擎中得到搜索到的单词,问题是Sphinx以小写形式返回输入的/machdd单词。

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

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

*注意mango中的m变为小写

所以我的问题是如何突出显示单词,即包装<strong>&</strong>围绕与搜索词匹配的词?而不丢失其文本框?

*ppl。它和如何突出显示搜索结果的问题不同,我问我的关键字数组是小写的,使用上面的方法,原始单词被小写单词取代。那我该怎么阻止呢?另一个问题链接也将面临这种情况,因为搜索到的关键字都是小写的。并且使用CCD_ 3将匹配它并用小写单词替换它。


更新:

我组合了各种代码片段,以获得我期望的代码。,就目前而言,它运行良好。

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

Regex解决方案:

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

只需小心在$searched_word 中可能被解释为元字符的特殊字符

这是我不久前写的一段代码片段,它正致力于做你想做的事情:

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);
}
//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中强大,同时保留原始文本大小写。

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

在此处检查原始链接