Click here to Skip to main content
5,790,650 members and growing! (18,201 online)
Email Password   helpLost your password?
Web Development » ASP » General     Intermediate

Keyword highlighting in ASP

By Konstantin Vasserman

A simple function to highlight keywords in ASP
VBScriptNT4, Win2K, Windows, ASP, IIS, Dev

Posted: 15 Jun 2000
Updated: 15 Jun 2000
Views: 87,874
Bookmarked: 35 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
21 votes for this Article.
Popularity: 4.97 Rating: 3.76 out of 5
1 vote, 100.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5

Sample Image - Highlight.gif

Introduction

While working on the my version of the message board ASP scripts I have found myself in the 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>")

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Konstantin Vasserman



Occupation: Web Developer
Location: United States United States

Other popular ASP articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Questionsummrizing the result [modified]memberferas.a4:03 30 May '06  
QuestionReplace in HTML elementsmemberarbpen7:11 4 Feb '06  
GeneralHighlighting and AnchoringsussAnonymous11:17 26 Jul '04  
GeneralHow to highlight Multiple Keywords using this Function?sussTommyX17:40 17 Jun '03  
GeneralRe: How to highlight Multiple Keywords using this Function?sussAnonymous8:17 23 Sep '03  
GeneralRe: How to highlight Multiple Keywords using this Function?sussAnonymous19:08 8 Jan '04  
GeneralHighlighting Options in a combo boxmemberAnonymous5:45 20 May '02  
Generalwhy not regular expression?memberinternal20:55 6 Feb '02  
GeneralRe: why not regular expression?membermsraot7:16 5 Jul '04  
GeneralRe: why not regular expression?memberinternal17:54 6 Jul '04  
Generalkeyword in HTML tags ?memberThomas Parvais12:25 2 Feb '01  
GeneralRe: keyword in HTML tags ?memberKonstantin Vasserman12:36 2 Feb '01  
GeneralRe: keyword in HTML tags ?memberModemMike18:50 15 Apr '02  
GeneralRe: keyword in HTML tags ?memberKonstantin Vasserman19:10 17 Apr '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Jun 2000
Editor: Chris Maunder
Copyright 2000 by Konstantin Vasserman
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project