繁体   English   中英

突出显示多个高级关键字

[英]Highlight multiple keywords advanced

我尝试了此处回答的大多数解决方案,但所有解决方案都存在相同的问题,这是我在这里提出的问题。

我将此功能用于高亮度搜索结果:

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

当我用2个或多个用空格分隔的字符串搜索文本并且它们中的任何一个具有突出显示的数组中的HTML代码时,都会发生问题。

例如,searchtext =“我具有最高系统性能”和searchstrings =“ max f”

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

在第二次迭代中,它将用<font color='#00f'><b>f</b></font>替换每个f

第二次迭代也将替换第一次替换中插入的html标签! 因此,它也将替换字符串<font color='#00f'> f吗?

有什么建议吗? 谢谢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));
?>

尝试以下

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

也许这对您来说是一个很好的解决方案?

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


?>

您需要在页面上添加一点CSS:

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

输出:我有每个F ormance Max系统

---

更新:如果您想突出显示完整的单词,请查看以下内容:

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

输出:我拥有最大的系统性能

我可能无法完全理解您的问题,但是我想您想突出显示搜索字符串中的每个匹配单词。

您可能只需要执行以下操作:

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

这将输出: “我具有最高的系统性能”

由于“ f”无法匹配

编辑 -这是如果您也想匹配单词的一部分。

有点丑陋,但我相信这会为您服务

$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