At the moment I have programmed an ajax search which is returning names. So far everything is working. However, I want to highlight the searched char/string simply with a <strong>
tag but keep the case as I have got it from the SQL:
Example:
Search Term: M
SQL result: 1. Max Mustermann, 2. Tom Maier
What I want:
<b>M</b>ax <b>M</b>uster<b>m</b>ann
Tom <b>M</b>aier
Means, I wanna keep the case as I get it from the database, just put tags around the char or string.
This is what I have so far, but it is not what I want. If I search a "m", the first char is also lower case although it is actually uppercase in the database.
public function highlightKeyword($haystack, $needle)
{
$string = mb_strtolower($haystack);
$string = $this->mb_ucfirst($string);
$string = preg_replace("/(".$needle.")/i", sprintf("<strong>".strtolower($needle)."</strong>"), $string);
return $string;
}
public function mb_ucfirst($string, $encoding)
{
$strlen = mb_strlen($string, $encoding);
$firstChar = mb_substr($string, 0, 1, $encoding);
$then = mb_substr($string, 1, $strlen - 1, $encoding);
return mb_strtoupper($firstChar, $encoding) . $then;
}
Any further ideas?