Languages ASP / VbScript Strings  

Advanced Highlight

download download home home  
important code information
Matt Alexander
minimum requirements:
Advanced Highlight description

This function searches through the string passed to it for either a string or array of keywords. It highlights the keywords found in the string by wrapping them in HTML <span> tags. Features: - If an array of keywords is passed to it, it highlights the matches in the order of longest keyword to shortest. - Safeguards against embedding highlight spans within existing highlight spans. - Uses regular expressions to replace keywords, so the resulting string does not change case. - Very fast. - I included lots of comments and a sample search form.

***** This ReadMe was last updated October 7, 2004.

***** Advanced Highlight is copyright 2004 by Matthew W Alexander of Orlando, Florida.
***** I offer it up for all to modify and use free of charge.
***** I only ask that you be kind enough to give me credit if you use it.

Contents of this Readme file:
1) What AdvancedHighlight.asp does
2) Side effects
3) How to use AdvancedHighlight.asp:
4) Version History


1) What AdvancedHighlight.asp does:

The highlight function searches through the string passed to it for either a string or array of keywords. It highlights the keywords found in the string by wrapping them in HTML tags. If an array of keywords is passed to it, it highlights the matches in the order of longest keyword to shortest.

The only way to highlight the keywords is to wrap them in HTML tags. I chose to wrap them in span tags, like this:

<span class='hl'>word</span>

And then I style the 'hl' class using CSS like this:

<style type="text/css">
span.hl { background-color: yellow; }

Once these HTML tags are inserted into the string, it is important not to let the next keyword be found inside the tags, or the tags will be broken.

For instance, observe the following example:

StringToHighlight = "This is a test"
kwdStringOrArray = Array("is", "pan")

StringToHighlight = highlight(StringToHighlight, kwdStringOrArray)


Without my safeguards, the StringToHighlight variable would now contain this string:

"This <s<span class='hl'>pan</span> class='hl'>is</s<span class='hl'>pan</span>> a test"

As you can see, there are spans embedded within spans. Try this with a whole paragraph of text and several keywords. it gets very ugly. The text that is written to the screen when you Response.Write the variable is usually truncated at the first embedded tag. So, all that would be written to the screen is "This " because the browser doesn't know how to parse the broken HTML.

Advanced Highlight does not allow fragmentation of the HTML tags that it inserts. It uses regular expressions to exclude keywords that might be found inside its spans, unless it can specify puctuation preceeding or following the keyword. For instance, the search will highlight "class" somewhere in the string, but will exclude "class=".

Advanced Highlight will not safegaurd against fragmentation of other HTML tags. If there are "" tags in the string to be searched, and you search for the letter "i", the span tags will be inserted inside the "" tags and run into the same problem as above. I suggest stripping out all HTML tags before calling the highlight function. In my AdvancedHighlight-testform.asp I used a regular expression called "objStripHTMLRegExp" to strip out all HTML tags before calling the highlight function.

Because I use regular expressions to replace the keywords, the replace is not case-sensitive. This prevents the highlight function from changing the case of the keyword match in the string to be searched, which the standard VisualBasic Replace() function does.


2) Side effects:
There is one condition when a keyword will not be highlighted:

a) Another keyword must have already been found and highlighted in the StringToHighlight.
- And -
b) This keyword exists two times in the following string: "<span class='hl'></span>".

This is to prevent the existing span tag from being fragmented by embedding another span within it. The broken HTML would not be displayed in the browser correctly if this were allowed.

This side effect of certain words or letters not being highlighted will probably only occur in 0.1% of all searches and will probably never be noticed by the user.

An example of this is when a user searches for both "span" and "pan" in the same search. "Span" will be highlighted but "pan" won't. The longest keywords are searched for first, so "span" will be highlighted. "Pan" will not be highlighted because of condition "b" above.


3) How to use AdvancedHighlight.asp:

1) unzip the files
2) put AdvancedHighlight.asp with your site files
3) You can put the AdvancedHighlight-TestForm.asp page in the same folder as the AdvancedHighlight.asp page to see the script in action right away.

- Once you are ready to use the script on your page: -

3) add this line of ASP to the top of the page that will display your highlighted output:

<!--#include file="AdvancedHighlight.asp" -->

Make sure you put the correct file path in this include statement. You may have something like:

<!--#include file="../myFolder/AdvancedHighlight.asp" -->

Keep in mind that you cannot use an absolute path in an include statement; it must be a relative path.

4) You can now use the highlight() function. Just pass it the string you want to output and the keyword string or array of strings you want to highlight within that output. Like this:

Response.Write(highlight(strMyParagraph, aryMyKeywords))
- OR -
strOutput = highlight(strMyParagraph, strMyKeyword)

5) After calling the highlight function, the intKeywordMatches variable will hold the number of keywords matched until the next time the highlight function is called. It does not keep track of how many times the keywords were highlighted, just the number of keywords that were highlighted. This is demonstrated in the AdvancedHighlight-TestForm.asp page.


4) Version History:

1.1) "intKeywordMatches" is a new variable that can be accessed from your search form ASP page. It totals the number of words highlighted in the string you are searching through. It does not total the number of times that the keywords were highlighted, just the number of words highlighted. It starts over at 0 every time the highlight function is called. So, you will have to create your own variable if you want to keep a grand total. This is demonstrated in the updated AdvancedHighlight-TestForm.asp page.

1.2) Characters that are special to regular expressions, specifically "$ . \ ? ^ * + | ( ) [ ] { }", are now properly escaped so that they too can be highlighted. This means that you can now highlight "C++" in your search results! I also added ability to highlight double-quotes. This required a small change to the AdvancedHighlight-TestForm.asp page.


Thank you for trying my code!

File List:

Similar code
Advanced HTML Encoder (Popularity: ) : This code demonstrates how you can convert a string to HTML text format. It goes beyond the power of the Server.HTMLEncode method in that it parses out all HTML tags, changes new-line characters to "BR" tags, and also formats double-spacing ...
Numbers to Words (Popularity: ) : This Code convert numbers (up to 7 digits and without decimals) to corresponding words. The grouping format is Indian, but can be easily changed to accommodate other formats as well.
Ghost text writter (Popularity: ) : The project will make text type and look like a ghost is typing. The speed of type is random so its more true.
WordWrap functions (Popularity: ) : For some purposes you need to cut a long string (with no HTMLTAGS) into (row)parts on a specific length. If you want to do this without APIs or DLLs and you don't want to break in the middle of words, ...
User reviews

Write a review:
1 2 3 4 5 6 7 8 9 10
1=poor 10=excellent
Write review*
Your name*
  (Comments are moderated, and will not appear on this site until the editor has approved them)
Rate me
supported os
downloads 96
size in Kb 7
popularity   2455/7916504
user rating 7/10

New Code
Popular Code