简体   繁体   中英

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.

For example, searchtext="I have max system performance" AND searchstrings="max f"

In first iteration foreach will replace every max with <font color='#00f'><b>max</b></font>

In second iteration it will replace every f with <font color='#00f'><b>f</b></font>

Second iteration will also replace html tags inserted in first replacement! So it will replace f in string <font color='#00f'> also?

Any suggestion? Thanks 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:

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

Outputs: I have max system per f ormance

---

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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