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 ... 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 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) %> | |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
| | | | IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
| | | | Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
| | | | Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
| | | | Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
| | | | Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |