PHP:如何突出显示搜索结果中的单词并保留原始文本大小写?

时间:2015-09-08 08:41:31

标签: php str-replace highlighting

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

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

e.g。

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以小写字母返回输入/输入的单词。

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

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

*注意芒果的m得到小写。

所以我的问题是如何突出显示单词,即包裹<strong>&amp; </strong>围绕与搜索词匹配的单词? 没有丢失它的文本包?

*脂肪酶。与how to highlight search results不同的问题,我问我的关键字数组是小写的,使用上面的方法,原始单词被小写单词替换。 那我该怎么办呢? 另一个问题链接也将面临这一点,因为搜索的关键字是小写的。并使用str_ireplace它将匹配它并用小写字替换它。

更新:

我已经结合了各种代码片段来获得我期望代码要做的事情。 现在它的工作很棒。

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

4 个答案:

答案 0 :(得分:1)

正则表达式解决方案:

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

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

答案 1 :(得分:1)

这是我之前写的一段代码片段,它正在努力完成你想要的工作:

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

答案 2 :(得分:0)

{{1}}

此代码将查找所有单词的出现次数,并在保留原始文本的情况下使其在html中变得强大。

答案 3 :(得分:-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

相关问题