Here’s a PHP function for a keyword search that will highlight all occurrences of a single keyword in a text string, whilst maintaining correct capitalisation.
Previously I used this simple snippet, that highlighted the searched word in a string. It uses inline styling for the highlight.
<?php $str = "The monkey hangs on the door" $keyword = "the" echo str_ireplace($keyword, '<span style="color: #daa732;">'.$keyword.'</span>', $str); ?>
The problem with this approach was that the resulting highlighted term uses the capitalisation of the search word, rather than the case of the original string. In the example above, the first word ‘The’ in the string would appear as a lower-case ‘the’
To solve this capitalisation problem, I created the below function which basically loops through all occurrences of the keyword within a string, forming a new highlighted string whilst maintaining the original capitalisation of the matched words it finds. It does this using a series of PHP string manipulation functions.
<?php $string = "The monkey hangs from the door"; $keyword = "the"; function highlightkeyword($str, $search) { $highlightcolor = "#daa732"; $occurrences = substr_count(strtolower($str), strtolower($search)); $newstring = $str; $match = array(); for ($i=0;$i<$occurrences;$i++) { $match[$i] = stripos($str, $search, $i); $match[$i] = substr($str, $match[$i], strlen($search)); $newstring = str_replace($match[$i], '[#]'.$match[$i].'[@]', strip_tags($newstring)); } $newstring = str_replace('[#]', '<span style="color: '.$highlightcolor.';">', $newstring); $newstring = str_replace('[@]', '</span>', $newstring); return $newstring; } ?>
That’s better, using this function, the matched keywords now have correct capitals.
To break down what’s going on here:
To use this function, simply pass the string as the first parameter and the search word as the second:
echo highlightkeyword($string, $keyword);
An obvious example of where you might need this highlighting function would be within a product search. The $keyword parameter would then become the search field name from the form, e.g. $_POST[‘keyword’] and the string would be whatever content you want highlighting such as the product title.
… Your comment system stripped out the tags:
function highlightKeyword2($haystack, $needle, $color = ‘#F00’)
{
return preg_replace("/($needle)/i", sprintf(‘<span style="color: %s">$1</span>’, $color), $haystack );
}
Thanks a lot Sam, that worked like a charm.
Tom, with your function
“`php
highlightkeyword($str, $search)
“`
span tag seems nested as many times as the number of occurrences in each location.
Thank u so mach dude, It was working so good
thank you so much……….. 🙂
Kindly tell us if the keyword is a Array of below elements…
$keyword = array(‘dot in’,’dot com’,’.in’);
How did i modify your code?
This completely breaks formating. No more line breaks, nothing.
How to find many keywords ? Thanks
hi, this code really worked well for me and also has the answer for my question in stackoverflow https://stackoverflow.com/questions/62931433/getting-the-code-executed-error-while-trying-to-highlight-some-words-in-php
Hi,
That is quite a long winded way of doing it! You can use a back reference to preserve capitalisation. e.g. This does the same but is much cleaner:
function highlightKeyword2($haystack, $needle, $color = ‘#F00’)
{
return preg_replace(“/($needle)/i”, sprintf(‘$1’, $color), $haystack);
}