PHP highlight search keywords

PHP highlight search keywords

While performing a search, it’s ofter very useful to highlight in different colors, backgrounds or styles the search keywords in the results returned. This keeps focus on the keywords and helps scan the page faster to find the more relevant results.

But how exactly can that be done? As a basic idea, the following code ( a PHP class ) practically takes as parameters the text that needs to be highlighted and the particular keywords that need to stand out. Using a regular expression, every keyword in the text is replaced with a span HTML element having different styles applied according to the keyword it matches.

Please remember that the code below is just an example, it can be and should be extended; I’m only presenting it for demonstration and “how to” purposes.

The class is made out of 3 methods: the class constructor, the hex color generator and a RGB to hex helper function. All the keywords highlighting is done in the class constructor that calls the hex color generator or the colors and than saves the highlighted text into the output variable.

But enough talk. Here’s the full code for highlighting the search keywords in a page based on what a user might be looking for on your website.

That would be it. This can be used in various ways: to highlight search from the search form of your own website or to highlight keywords for users that come from Google, Yahoo or other web search engine.

You have a point here. If the text you’re highlighting has HTML markup, it will create a mess. I developed this script for plain text so a solution would be to use strip_tags() on the text and after that do the highlighting.

This happens because the script highlights all words you entered, in this case “apple” and “a”. One thing you could do is check string length and apply highlight only if word has more than x letters ( x being the minimum number of letters you want the script to consider a word ).

This happens because words in string get replaced with words from query. So if your user enters words in uppercase, words in your text will be replaced by the uppercase version from query. You could make the search case sensitive by removing the i in preg_replace ( preg_replace(“|($word)|U”… )

Hi guys! First of all, good work! I’m trying to create a similar code, with str_replace. I don’t know exactly what the difference is, but i does no matter. The Problem I have, and this code has also, (and haven’t found a solution for this yet), if you type in some of the following keywords: span, style, backgroung etc. the result breaks. For example:
$highlight = new highlight($text , ‘lorem dolor back’);
it’s a big problem i think. And it is caused by the foreach() function.
First it replaces the words ‘lorem’ to: lorem
Then it searches the word ‘back’ and replaces it, but it searches in the previous text with the HTML tags.
So the result HTML code is that:
<span style=\"background:#AD7CA7;\”>lorem

Hmm, no solution for that yet. This is a very old example about what can be done with regular expressions and it’s for demo purposes only. I’ll try and expand that a little, but this will probably happen during the next year, enjoying holidays now.

This solution seams to have resolved your issue. If you concatenate the string into a new string you eliminate the issue of searching through the new html text that you create in the foreach loop. Then I cleared the $text to prevent any other unforeseen issues.

This tutorial is definitely the best I’ve seen for highlighting search strings in search results because it actually replaces the strings with the correct casing. Many of the other methods I’ve seen online replace the result text with the casing that was typed in the search.

The only issue I had to deal with was if someone started typing nothing but spaces. It gets a little crazy.