i\'m using this code to highlight search keywords:
function highlightWords($string, $word)
{
$string = str_replace($word, \"
as suggested by user187291, just change following code in order to get text highlighted with yellow background.
return preg_replace($re, '<SPAN style="BACKGROUND-COLOR: #ffff00"><b>$0</b></SPAN>', $text);
The other solutions may be case-insensitive in finding the highlight terms, but do not preserve their case of the original string. So searching for "st" will find "ST" but highlight it as "st", the search term.
I use the following. It first forms the replace array, and then uses str_replace() with array parameters - which avoids recursion.
function highlightStr($haystack, $needle, $highlightStyle) {
if (strlen($highlightStyle) < 1 || strlen($haystack) < 1 || strlen($needle) < 1) {
return $haystack;
}
preg_match_all("/$needle+/i", $haystack, $matches);
$matches[0] = array_unique($matches[0]);
if (is_array($matches[0]) && count($matches[0]) >= 1) {
foreach ($matches[0] as $ii=>$match)
$replace[$ii]="<span style='$highlightStyle'>$match</span>";
$haystack = str_replace($matches[0], $replace, $haystack);
}
return $haystack;
}
I've used the regex written before and replaced \w
with [A-Za-z0-9_äöüÄÖÜ]
. As you see I added the umlauts äöüÄÖÜ
.
I also have removed the \b
so it will match any appearance of the search term.
search term:
Su shamp
text:
Sun shiny shampoo
result:
Sun shiny shampoo
private function getSearchTermToBold($text, $words)
{
preg_match_all('~[A-Za-z0-9_äöüÄÖÜ]+~', $words, $m);
if (!$m)
return $text;
$re = '~(' . implode('|', $m[0]) . ')~i';
return preg_replace($re, '<b>$0</b>', $text);
}
Splits your search query up into words, then highlight each words separately.
It might work out better to perform the highlighting in javascript though. jQuery's "contains" selector will probably help avoid problems of replacing markup elements as you go...
http://api.jquery.com/contains-selector/
regular expressions is the way to go!
function highlight($text, $words) {
preg_match_all('~\w+~', $words, $m);
if(!$m)
return $text;
$re = '~\\b(' . implode('|', $m[0]) . ')\\b~';
return preg_replace($re, '<b>$0</b>', $text);
}
$text = '
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
';
$words = 'ipsum labore';
print highlight($text, $words);
To match in a case-insensitive manner, add 'i' to the regular expression
$re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';
NB: for non-enlish letters like "ä" the results may vary depending on the locale.
PHP > 5.3.0, try preg_filter()
/**
* Highlighting matching string
* @param string $text subject
* @param string $words search string
* @return string highlighted text
*/
public function highlight($text, $words) {
$highlighted = preg_filter('/' . preg_quote($words, '/') . '/i', '<b><span class="search-highlight">$0</span></b>', $text);
if (!empty($highlighted)) {
$text = $highlighted;
}
return $text;
}