How do you highlight searched words in results?
This is a simple matter of finding the text you are looking for, and replacing this text with a highlighted version of the same text. This can be accomplished using the replaceNoCase() function.
<cfset myWord = "my">
<cfoutput>#replaceNoCase(myText,myWord,"<span style='background:yellow'>#myWord#</span>","all")#</cfoutput>
Also, Verity searches support this functionality right out of the box using the context column. In your Verity result set, the context column returns a context summary containing the search terms, highlighted in bold. (This is enabled if you set the contextpassages attribute to a number greater than zero.). If bolding your search term is not enough, you can use the contextHighlightBegin and contextHighlightEnd attributes to append custom HTML before and after your search term.
This question was written by Jeremy Petersen.
It was last updated on June 30, 2006 at 1:47:58 PM EDT.
CFML Referenced
Categories
Comments
Comment made by Adam Fairbanks (Tidy Technologies) on May 29, 2006 at 3:07 AM
A simple replace is easy. But what if the content is HTML and the search phrase is in a tag? How do you ignore html tags? So that
<div id="my">
does not become
<div id="<span style='background:yellow'>my</span>">
?
Comment made by Jeremy Petersen on May 30, 2006 at 11:55 AM
You will need to use regex to ignore the tags. Something like:
http://concepts.waetech.com/replace_inner_text/
Comment made by Teddy R Payne on June 4, 2006 at 1:40 PM
In your search patterns, just could just simple use: [\s]#myword#[\s]
This would give you the word when it has a single space before and after it as if it were in the middle of a sentence.
Comment made by Dave on October 20, 2006 at 10:30 AM
use the HighLight UDF from cflib.org
http://cflib.org/udf.cfm?ID=133