Search and Replace function by Meraj Sami

Search and Replace by Meraj SamiThis one came about helping one of the users on one of the message board. He had asearch page from which the users on his site, guess what, could do the search. He wantedto highlight the search criteria in his result from the database. For example if theuser's search string was "the" and one of the string i ...

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
May 18, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Search and Replace by Meraj Sami

This one came about helping one of the users on one of the message board. He had asearch page from which the users on his site, guess what, could do the search. He wantedto highlight the search criteria in his result from the database. For example if theuser's search string was "the" and one of the string in the search resultcontained (remember, it is an example) "whatever summer winter tHe weather whetherTHE the The tHe thE thermometer thermometerthe", every occurrence of "the"should be highlighted. If you do it with replace function, for example,


<%
searchString="the"
replaceWith="<FONT COLOR='red'><b>" & searchString & "</b></font>"
strOriginal="whatever summer winter tHe weather whether THE the The tHe thE thermometer thermometerthe"
strOriginal=Replace(strOriginal,searchString,replaceWith,1,-1,1)
%>
then you can highlight every occurrence of the search string in the result, but in the process lose the case of the search string -- all the occurrence of "the" now will
be in lower case. To avoid that I came up with this little function that will highlight all the occurrence of the search string in the result and also maintain the original case.
<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Function stringReplace(strSearchWithin,strSearchFor)
Dim lngStartingPosition
Dim lngFoundPosition
Dim strReplaced
'Set the start position
    lngStartingPosition=1
    lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
    do while lngFoundPosition > 0
        'found
        strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='red'>" & mid(strSearchWithin,lngFoundPosition,len(strSearchFor)) & "</font>"
        lngStartingPosition=lngFoundPosition+len(strSearchFor)
        lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
    Loop
    stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
End Function
</SCRIPT>
<%
OPTION EXPLICIT
Dim strSearchWithin,strSearchFor
strSearchWithin="whatever summer winter tHe weather whether THE the The tHe thE thermometer thermometerthe"
strSearchFor="the"
Response.Write stringReplace(strSearchWithin,strSearchFor)
%>
blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials