Highlighting Search Terms
faq232-1167
If you search for "time" using a search utility, how do you highlight
the word "time" throughout the results, without also highlighting "sometime"
or "timeframe" or other variations? The trick is to use Regular
Expressions, or pattern matching.
This assumes someone submitted a form with a keyword to be searched.
These tags work on the results before they are displayed.
<cfset keyword = FORM.keyword>
<cfset oldResults = VARIABLES.results>
<cfset newResults = REReplaceNoCase(VARIABLES.oldResults,
"(([^A-Za-z])(#Trim(VARIABLES.keyword)#)([^A-Za-z]))", "\2<b
style='background=color:yellow'>\3</b>\4", "ALL")>
<cfif Left(VARIABLES.newResults, Len(Trim(VARIABLES.keyword))) IS
Trim(VARIABLES.keyword)>
<cfset newResults = REReplaceNoCase(VARIABLES.newResults,
"((#Trim(VARIABLES.keyword)#)([^A-Za-z]))", "<b
style='background-color:yellow'>\2</b>\3", "ONE")>
</cfif>
What does the \2 \3 and \4 mean? These are numbers to references
pieces of the input to a regular expression function like REReplaceNoCase().
For example, see the following:
(\1 (\2) (\3) (\4 (\5) ) )
Look at the placement of parentheses and numbers, \1 starts at the
outermost pair of parentheses, and working your way in increases every
number. If they are on the same level of the hierarchy of parentheses, it
goes from left to right in the expression.
Dave
----- Original Message -----
From: "Tuan" <[EMAIL PROTECTED]>
To: "CF-Talk" <[EMAIL PROTECTED]>
Sent: Friday, February 14, 2003 6:18 PM
Subject: Re:RE: RE: Highlighting search keyword problem
> Does anybody else have any other suggestion or alternatives. I've thought
about doing this before, but that would mean I would have to edit a lot of
templates for this highlighting function to work.
>
> ---------- "Raymond Camden" <[EMAIL PROTECTED]> writes:
>
> So basically your search returns matches as ids, and you link to
> article.cfm?id=X or some such? If so, in your link, simply pass along
> the search terms again. On your article.cfm, do (pseudo-code)...
>
> <cfset data = the content from the database>
> <cfif isDefined("url.searchTerms")>
> <cfset data = use replace to replace searchterms matches with
> <font color='red'>#searchterm...
> </cfif>
>
> <cfoutput>
> #data#
> </cfoutput>
>
>
> =======================================================================
> Raymond Camden, ColdFusion Jedi Master for Mindseye, Inc
> Member of Team Macromedia
>
> Email : [EMAIL PROTECTED]
> Blog : www.camdenfamily.com/morpheus/blog
> Yahoo IM : morpheus
>
> "My ally is the Force, and a powerful ally it is." - Yoda
>
> > -----Original Message-----
> > From: Tuan [mailto:[EMAIL PROTECTED]]
> > Sent: Friday, February 14, 2003 4:01 PM
> > To: CF-Talk
> > Subject: Re:RE: Highlighting search keyword problem
> >
> >
> > Well, my document template is pulling the data from the
> > database but its not using the query thats run when the
> > search function is started.
> >
> > ---------- "Barney Boisvert" <[EMAIL PROTECTED]> writes:
> >
> > Are you saying that your document templates contain the
> > content directly, rather than pulling it from the database?
> > If that's the case, you're stuck, unless you somehow read
> > that template into a variable so you can manipulate it (with
> > replaceNoCase).
> >
> > > -----Original Message-----
> > > From: Tuan [mailto:[EMAIL PROTECTED]]
> > > Sent: Friday, February 14, 2003 1:32 PM
> > > To: CF-Talk
> > > Subject: Highlighting search keyword problem
> > >
> > >
> > > Ok, I've searched and read many topics on how to highlight keywords
> > > from a search result, but im still having problems figuring
> > this out.
> > > All of the topics and examples that i've seen shows how to do it if
> > > your search result(detailed) page is created by ur search query. If
> > > thats the case then you would use the ReplaceNoCase
> > function on your
> > > search query. Or other examples showed that the text was
> > already set
> > > to a variable which then was used by the ReplaceNoCase function to
> > > highlight the text.
> > >
> > > But my problem is what if in your application you have a search
> > > function that runs and the search results shows a list of
> > doc titles
> > > that contains the keyword that the user searched. Now each
> > doc title
> > > most likely has a different template name associated with it. When
> > > you click on the title it will take you to different templates that
> > > existed previously w/o having any relationship with the
> > search query
> > > at all (except for it contained that keyword). How can i apply the
> > > highlighting of the keywords to these already existing
> > documents? I'm
> > > sorry if this isn't very clear.
> > >
> > >
> > >
> > >
> >
> >
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Signup for the Fusion Authority news alert and keep up with the latest news in
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4