VB icon

Word Highliter

Email
Submitted on: 1/5/2015 8:38:00 PM
By: masswebsites (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: ASP (Active Server Pages)
Views: 610
 
     Ever see a search engine that highlights your search results? This code does that. It takes a querystring, and compares it to a second variable. The second variable will be written to the page, and any matching words will be in highlighted and bolded. See screenshot for details.

 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Word Highliter
'**************************************
Have fun.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Word Highliter
' Description:Ever see a search engine that highlights your search results? This code does that. It takes a querystring, and compares it to a second variable. The second variable will be written to the page, and any matching words will be in highlighted and bolded. See screenshot for details.
' By: masswebsites (from psc cd)
'
' Inputs:request.querystring("qs")
'
' Returns:Returns a string with highlighted words that are similar.
'
' Assumes:Assumes your querystring and compare variable are delimited by spaces. This can easily be changed. You could also replace the querystring input with a value from a database, an xml file, or anything else. Pleasse comment.
'
' Side Effects:Currently Unknown. This has also not been user load-tested.
'**************************************

<%
' hit highliter
' this was not user load tested
' have fun - masswebsites@yahoo.com
option explicit
Response.Expires=0
%>
<html>
<head>
<title></title>
</head>
<body>
	<%
	' check querystring
	if request("qs") = "" then
		Response.Write "No querystring supplied. <a href='hitliter.asp?qs=asp%20loop%20example'>Try this one</a>"		
	else
		dim sQueryString
		dim sSummary
		dim sDisplay
		
		sQueryString = request("qs")
		
		' example summary - could also be from a database or other data source
		sSummary = "I hope you find this ASP code sample useful. It uses nested for next loops , strcomp , arrays , join , split , and some other fun things. If you can comment, criticize, or optimize this code example, please post a reply."
		
		' display with matching words highlighted. Could also set a variable = hitLite(sSummary,sQueryString)
		Response.Write hitLite(sSummary,sQueryString) & "<br><br>"
	end if
	%>
</body>
</html>
<%
' **************************************
' put it in a global include file
' **************************************
function hitLite (sSummary,sQueryString)
	dim arrQueryString
	dim iQs
	dim i
	dim arrSummary
	dim sArrQueryStringTmp
	dim iComp
	dim sArrSummaryTmp
	dim sRoot
	dim sLastChar
	dim sLength
	
	' break the variable we want to compare querystring to into an array
	arrSummary = split(sSummary," ")	
		
	' break querystring into an array, use space for delimiter
	arrQueryString = split(sQuerystring," ")
		
	' for every word in the querystring
	for iQs = 0 to ubound(arrQuerystring)
			
		' assign the value to a temp variable
		sArrQueryStringTmp = arrQuerystring(iQs)
			
		' don't include common search words, you can take this out. I was using this for a search engine
		if (sArrQueryStringTmp <> "and") and (sArrQueryStringTmp <> "or") and (sArrQueryStringTmp <> "+") then
				
			' for each element in the variable array, replace querystring word in varaible array with the word plus bgcolor ( highlight )	
			for i = 0 to ubound(arrSummary)
				
				sArrSummaryTmp = arrSummary(i)
					
				' if the 2 strings compare, stick the display value in a span with style! 
				if strcomp(sArrSummaryTmp,sArrQueryStringTmp,1) = 0 then
					arrSummary(i) = "<span style=background:yellow;font-weight:bold;>" & sArrSummaryTmp & "</span>"		
				else
					' check "s", comma, period
					' must end "s" or comma or period AND be greater than 1 character
					sLastChar = right(sArrSummaryTmp,1)
					sLength = len(sArrSummaryTmp)
				
					if (sLastChar = "s") or (sLastChar = ".") or (sLastChar = ",") and (sLength > 1) then
						
						'the word minus last letter
						sRoot = left(sArrSummaryTmp,sLength-1)
						
						' if the root comapres to the querystring
						' replace that element of the array with the root highlited and the last character in regular display
						if strcomp(sRoot,sArrQueryStringTmp,1) = 0 then
							' don't include the comma or period in the lite
							arrSummary(i) = "<span style=background:yellow;font-weight:bold;>" & sRoot & "</span>" & sLastChar & " "		
						end if
					end if
				end if
				
			next
		
		end if
		
	next
	hitLite = join(arrSummary, " ")
		
end function
%>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.