highlight some texts in a search result
Posted: Wed Aug 29, 2007 4:10 pm
Hi everyone. I have created a search function for my website where I search the entire database for a user inputted keyword.
I store the results to a variable named "content" before echoing it. Now I want to highlight every character in "content" that matches the keyword. So far I have done the highlighting part but the problem is, I can't keep the case of the characters.
ex:
a keyword "php" can find the words "Php" but it won't highlight it as "Php". The "Php" becomes "php" when I use my highlighting script.
heres my code:
I might consider using a different approach if anyone has an Idea.
TIA
I store the results to a variable named "content" before echoing it. Now I want to highlight every character in "content" that matches the keyword. So far I have done the highlighting part but the problem is, I can't keep the case of the characters.
ex:
a keyword "php" can find the words "Php" but it won't highlight it as "Php". The "Php" becomes "php" when I use my highlighting script.
heres my code:
Code: Select all
function highlightKeywords($keyword, $contentString){
$finalString = str_ireplace($keyword, "<span class=\"keyword-text\">$keyword</span>", $contentString);
return $finalString;
}
echo highlightKeywords($keywordvariable, $content);TIA