Here is a little UDF I worked on this morning. I've had code like this in BlogCFC for a while, but I needed it in a UDF for my Picard project so I just whipped it out. The basic idea is:

You have a block of text of arbitrary text.
You had searched for something and that something is probably in the text. (I say probably because the search may have matched on another part of the content in question, like the title.)
You want to highlight the match in the content.
You also want to crop the content to X characters long, and if a match was found, center the X characters around the first match.

Make sense? So given a block of text, like the lyrics to Lady Gaga's "Poker Face" (don't ask), I can find/highlight the word poker like so:

#highlightAndCrop(text,"poker",250,"<b></b>")#

Where text is a variable containing the lyrics, poker is the word to highlight, 250 is the size of the result (which is a bit fuzzy, will explain why in a bit), and the final argument is the "wrap" to use around the result. Here is what the UDF will return:

... oh, oh, ohhhh, oh-oh-e-oh-oh-oh,
I'll get him hot, show him what I've got

Can't read my,
Can't read my
No he can't read my poker face
(she's got me like nobody)
Can't read my
Can't read my
No he can't read my poker face
(she's got me like nobody)

P-p-p...

So you get the basic idea. Here is the UDF (as it stands now, but there are parts of it I'd like to improve):

<cffunction name="highlightAndCrop" access="public" output="false" hint="Given an arbitrary string and a search term, find it, and return a 'cropped' set of text around the match."> <cfargument name="string" type="string" required="true" hint="Main blob of text"> <cfargument name="term" type="string" required="true" hint="Keyword to look for."> <cfargument name="size" type="numeric" required="false" hint="Size of result string. Defaults to total size of string. Note this is a bit fuzzy - we split it in two and return that amount before and after the match. The size of term and wrap will therefore impact total string length."> <cfargument name="wrap" type="string" required="false" default="<b></b>" hint="HTML to wrap the match. MUST be one pair of HTML tags.">

<cfset var excerpt = "">

<!--- clean the string ---> <cfset arguments.string = trim(rereplace(arguments.string, "<.*?>", "", "all"))>

<!--- pad is half our total ---> <cfif not structKeyExists(arguments, "size")> <cfset arguments.size = len(arguments.string)> </cfif> <cfset var pad = ceiling(arguments.size/2)>

<cfset var match = findNoCase(arguments.term, arguments.string)> <cfif match lte pad> <cfset match = 1> </cfif> <cfset var end = match + len(arguments.term) + arguments.size>

<!--- now create the main string around the match ---> <cfif len(arguments.string) gt arguments.size> <cfif match gt 1> <cfset excerpt = "..." & mid(arguments.string, match-pad, end-match)> <cfelse> <cfset excerpt = left(arguments.string,end)> </cfif> <cfif len(arguments.string) gt end> <cfset excerpt = excerpt & "..."> </cfif> <cfelse> <cfset excerpt = arguments.string> </cfif>

<!--- split up my wrap - I bet this can be done better... ---> <cfset var endInitialTag = find(">",arguments.wrap)> <cfset var beginTag = left(arguments.wrap, endInitialTag)> <cfset var endTag = mid(arguments.wrap, endInitialTag+1, len(arguments.wrap))>

<cfset excerpt = reReplaceNoCase(excerpt, "(#arguments.term#)", "#beginTag#\1#endTag#","all")>

<cfreturn excerpt> </cffunction>

For the most part this should make sense. I attempt to find the term within the string and use that as a base to create an excerpt. I handle cases where the match isn't found and I also handle cases where the total string is smaller than the crop. Note that the wrap HTML you include will have an impact on the total length of the string, but that shouldn't matter.

The main part I don't like is the wrap portion. It only supports one set of tags. I may split this into two arguments, a beginWrap and endWrap. For now though it suits my purposes.

p.s. This UDF is ColdFusion 9 only because of the var statements intermingled within the UDF. To use this in earlier versions, simply move the var statements to the beginning of the UDF.