简体   繁体   English

突出显示多个高级关键字

[英]Highlight multiple keywords advanced

I tried most of solution answered here but all of them have same problem which is my question here. 我尝试了此处回答的大多数解决方案,但所有解决方案都存在相同的问题,这是我在这里提出的问题。

I use this function for highligh search results: 我将此功能用于高亮度搜索结果:

function highlightWords($searchtext, $searchstrings){   
        $searchstrings = preg_replace('/\s+/', ' ', trim($searchstrings));
        $words = explode(' ', $searchstrings);

        $highlighted = array();
        foreach ( $words as $word ){
            $highlighted[] = "<font color='#00f'><b>".$word."</b></font>";
        }

        return str_replace($words, $highlighted, $searchtext);
    }

Problem occurs when i search text with 2 or more strings separated with spaces and any of them have any of HTML code from my highlighted array. 当我用2个或多个用空格分隔的字符串搜索文本并且它们中的任何一个具有突出显示的数组中的HTML代码时,都会发生问题。

For example, searchtext="I have max system performance" AND searchstrings="max f" 例如,searchtext =“我具有最高系统性能”和searchstrings =“ max f”

In first iteration foreach will replace every max with <font color='#00f'><b>max</b></font> 在第一次迭代中,foreach将用<font color='#00f'><b>max</b></font>替换每个最大值

In second iteration it will replace every f with <font color='#00f'><b>f</b></font> 在第二次迭代中,它将用<font color='#00f'><b>f</b></font>替换每个f

Second iteration will also replace html tags inserted in first replacement! 第二次迭代也将替换第一次替换中插入的html标签! So it will replace f in string <font color='#00f'> also? 因此,它也将替换字符串<font color='#00f'> f吗?

Any suggestion? 有什么建议吗? Thanks Miodrag 谢谢Miodrag

<?php
    $searchtext = "I have max system performance";
    $searchstrings = "max f";
    $searchstrings = preg_replace('/\s+/', ' ', trim($searchstrings));
    $words = explode(' ', $searchstrings);
    $highlighted = array();
    foreach ( $words as $word ){
        $highlighted[] = "<font color='#00f'><b>".$word."</b></font>";
    }
    echo strtr($searchtext, array_combine($words, $highlighted));
?>

Try the following 尝试以下

foreach ( $words as $word ){
   if(strlen ($word)>2)
   {
      $highlighted[] = "<font color='#00f'><b>".$word."</b></font>";
   }
}

Maybe this is good Solution for you? 也许这对您来说是一个很好的解决方案?

 function highlightWords($searchtext, $searchstrings){   
        $searchstrings = preg_replace('/\s+/', ' ', trim($searchstrings));
        $words = explode(' ', $searchstrings);

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

        return str_replace($words, $highlighted, $searchtext);
    }

echo highlightWords('I have max system performance', 'max f');


?>

You need to add a little bit CSS on your Page: 您需要在页面上添加一点CSS:

<style>
.highlighted-word {
    font-weight: bold;
}
</style>

Outputs: I have max system per f ormance 输出:我有每个F ormance Max系统

--- ---

UPDATE: If you like to hightlight the complete word, look at this: 更新:如果您想突出显示完整的单词,请查看以下内容:

function highlightCompleteWords($searchtext, $searchstrings){   
        $searchstrings = preg_replace('/\s+/', ' ', trim($searchstrings));
        $words = explode(' ', $searchstrings);


        $highlighted = array();
        foreach ( $words as $word ){
            $searchtext = preg_replace("/\w*?".preg_quote($word)."\w*/i", "<span class='highlighted-word'>$0</span>", $searchtext);
        }

        return $searchtext;
    }


echo highlightCompleteWords('I have max system performance', 'max f');

Outputs: I have max system performance 输出:我拥有最大的系统性能

I might not fully understand your question, but I guess you want to highlight every matched word in the search string. 我可能无法完全理解您的问题,但是我想您想突出显示搜索字符串中的每个匹配单词。

You could probably just do something like: 您可能只需要执行以下操作:

 $returnString = $searchtext;
    foreach ( $words as $word ){
       $returnString = preg_replace('/\b'.$word.'\b/i', "<font color='#00f'><b>$0</b></font>", $returnString);
    }
 return $returnString;

This would output: "I have max system performance" 这将输出: “我具有最高的系统性能”

Since the "f" wouldn't get matched 由于“ f”无法匹配

EDIT - This is if you wanna match part of a word as well. 编辑 -这是如果您也想匹配单词的一部分。

Kind of ugly but I believe this will fork for you 有点丑陋,但我相信这会为您服务

$returnString = $searchtext;
    foreach ( $words as $word ){
       if(strlen($word)>2){
          $returnString = preg_replace('/'.$word.'/i', "§§§$0###", $returnString);
       }
    }
$returnString = preg_replace("/\§§§/","<font color='#00f'><b>", $returnString);
$returnString = preg_replace("/\###/","</b></font>", $returnString);

return $returnString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM