FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

ASP Tutorial: Keyword Highlighting in ASP

jawahar
5/26/2005 7:12:04 AM, Views: 3267
Introduction

It would be important to highlight certain keywords in a message board for example or while searching for a keyword in ASP Search Engine, We are then in need of a function that would highlight certain keywords within the text of the messages.

My first idea was to use VBScript's built-in Replace() function. The only problem with Replace was that I was not able to preserve the original case of the letters within the text. Consider following example:

myText = "Using Replace function to make word REPLACE bold."
myText = Replace(myText, "replace", "<b>replace</b>", 1, -1, 1)


The resulting string will be "Using <b>replace</b> function to make word <b>replace</b> bold.". It did make all the words "Replace" bold, but it also changed the case of these words from their original case.

This is why I had to write this little Highlight function. There is nothing special about this function, but it does what I needed it to
do and I hope that you could use it somewhere in your ASP scripts as
well. So here it is:

' HIGHLIGHT function will search text for a specific string
' When string is found it will be surrounded by supplied strings
'
' NOTE: Unfortunately Replace() function does not preserve the original case of the found string. This function does.
'
' Parameters:
' strText - string to search in
' strFind- string to look for
' strBefore- string to insert before the strFind
' strAfter - string to insert after the strFind
'
' Example:
' This will make all the instances of the word "the" bold
'
' Response.Write Highlight(strSomeText, "the", "<b>", "</b>")
'
Function Highlight(strText, strFind, strBefore, strAfter)
Dim nPos
Dim nLen
Dim nLenAll

nLen = Len(strFind)
nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1
Highlight = strText
If nLen > 0 And Len(Highlight) > 0 Then
nPos = InStr(1, Highlight, strFind, 1)
Do While nPos > 0
Highlight = Left(Highlight, nPos - 1) & _
strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
Mid(Highlight, nPos + nLen)
nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
Loop
End If
End Function


To use this function - include it in your ASP scripts and call it like this:

Response.Write Highlight(myText, "someword", "<font color=red>", "</font>")
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Not Yet Rated!
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
ASP RSS Feed
Most Popular Tutorial
Most Popular Solution
Top Rated
Top Rankers
Overall
1. jawahar (2900)
Yearly -2008
1. jawahar (100)
Expertsforge Sponsors
bnrtop