40 LINES OF CODE --

OK, maybe I can learn something here that I don't know.

Here's what I was trying to do (in English): A search engine functionality; when the 
results list returned, the keyword (let's keep it to single word/phrase to simplify) 
found in the results should be highlighted in so way (bold, with a span with a class 
wrapper; whatever). The trick is that the highlighted text should keep the same case 
as it is in the ORIGINAL text, not the keywork text. In other words, if I search for 
"site", the highlighted text should be "Site" (uppercase "S") if it's "Site" in the 
original text. 

Sounds trivial, but with CF's standard replace function, its replace a string with a 
string, and there is no way to control case. This looks ugly in the results, to have, 
for example, uppercase "WEB" highlighted all over the place if that's what the user 
entered.

In PERL, here's the replace line:
$definition =~ s/$searchTerm/$subStart$&$subEnd/ig;

 -- Where $subStart is the begin span; $subEnd is close span. The "&" character 
between the two keeps the ORIGINAL text that matchs the keyword $searchTerm

IN PHP:
$r_title[$j] = eregi_replace($keyword, "<span 
style=background-color:#99CCFF;>\\0</span>", $r_title[$j]);

-- very similar to the Perl; I didn't (yet) set style to variables. Here, the "\\0" 
means -- like the Perl -- to keep the ORIGINAL text.

COLD FUSION: 
I had to set a loop and, when I found the text, do a LEFT and store it. Wrap the MID 
text (keyword) that matched with highlight tag, and set a new beginning for the search 
after the LEFT + len(tags added) + len(keyword).

If anyone has better code or ideas, I'm all ears. (P.S. Actually only 25 lines of 
actual code, not 40. While it still could be tuned, it still is WAY more than ONE line 
(PHP & Perl), and more cumbersome:

<!--- PAGE: /quote/highlight.cfm --->
<!--- 

CUSTOM TAG to higlight text search with keyword matches on /quote/results.cfm page

Need to have a custom tag, as CF provides to simple global to replace without changing 
text
case. For example, in Perl/PHP etc can change "My text" to "My <b>text</b>" even when 
the 
keyword is "TEXT" -- CF will only allow direct substitution, so the string to search 
will
become "My <b>TEXT</b>", which is NOT what is desired.

So have to loop.

INPUT required:
        -- text (text to search)
        -- keyword (substring to match on)
        
OUTPUT supplied:
        -- myHighlight (text highlighted appropriately)
        
Text colored with "highlight" class in a span; change in CSS file       

 --->



<cfparam name="attributes.text" default=""> <!--- default string to search --->
<cfparam name="attributes.keyword" default = ""> <!--- default keyword to use --->
<cfparam name="myHighlight" default = "">
<cfparam name="whereFound" default = 1> <!--- where keyword match found --->

<!--- colorizations --->
<cfset openSpan = "<span class='highlight'>">
<cfset closeSpan = "</span>">

<cfset text = #attributes.text#>
<cfset keyword = #attributes.keyword#>

<!--- see if loop is necessary  --->
<cfset whereFound = #findNoCase(keyword,text,1)#>


<!----------------------------------------- 
Enter and stay in loop as long as there is a match.

Each loop, the text string is rebuilt with the style
tags, and then the start point to do the match is
bumped up accordingly. 

So the full string is always fully there for
whenever the loop terminates
------------------------------------------>
<cfloop condition="#whereFound# GT 0">
        
        <!--- get what's NOT highlighted on LEFT --->
        <!--- different left if leads with match --->
        <cfif #whereFound# EQ 1> <!--- no left to pull --->
                <cfset left = "">
        <cfelse>
                <cfset left = #left(text,whereFound-1)#>
        </cfif>
        
        <!--- pull string to highlight (MID) --->
        <cfset mid = #mid(text,whereFound,len(keyword) )#>
        
        <!--- get leftover (RIGHT) if any --->
        <!--- test to make sure will not trying to get text past end of string --->
        <cfif #whereFound# + #len(keyword)# LTE #len(text)#>
                <cfset right = #right(text,len(text) - (whereFound + len(keyword) - 1) 
)# >
        <cfelse>
                <cfset right = ""> 
        </cfif>
        
        <!--- build new text string --->        
        <cfset text = #left# & #openSpan# & #mid# & #closeSpan# & #right#>
        
        <!--- set new starting point, taking into account text added (SPANs) --->
        <cfset whereFound = 
#findNoCase(keyword,text,whereFound+len(openSpan)+len(closeSpan)+len(keyword)  )#>
</cfloop>

<!--- Assign to variable that other pages will call --->

<cfset caller.myHighlight = #text# >

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
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
Your ad could be here. Monies from ads go to support these lists and provide more 
resources for the community. http://www.fusionauthority.com/ads.cfm

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to