facebook  linkedin  Twitter  skype  Rss googlePlus

The Replace Function

Dec 18 1999
5041

Formatting HTML
Have you ever had to enter information from an HTML form and place it into a database memo field only to display the information back to the user and try to format it the way they put it into your form field?


You could use the "


" tags to format the data so it's displayed exactly as the user entered it. The only problem using this method is it won't wrap the text to the next line. It will just display the text as one line and not start a new line unless the user actually placed a carriage return in the entered data. This could really distort your page and cause some really serious formatting problems.

The way around this is to use the VBScript Replace function in your ASP code to change each occurrence of a carriage return line feed into the equivalent html tag. This way it will handle the data as any other html text and wrap it when it needs to be wrapped and place a "

" or "
" tag where there needs to be one.


For example we look for the corresponding ascii character code and then replace it with the appropriate html tag:

strMemo = Replace(strMemo, Chr(13), "

")


Database Functions
The Replace function is a really nice function to have some other handy items for it are preventing your SQL statements from crashing when you try to insert a string into a field that contains an apostrophe, say a name like O'Hara. In order to filter for data like this use the Replace function to double them up:

strField = Replace(strField, "'", "''")

Now you have some simple insurance against one of the most common problems encountered by beginning ASP developers.


Building SQL Statements
Another item for using the Replace function, that I use is for capturing the space in an entered field for keyword searching and then replacing the space with the appropriate SQL code. Say a user enters two keywords in a search field like "ASP Engine", and you want to be able to do a search on a field for both of these values, or any of the values, or the exact phrase. How do you do this with minimal amount of code? Use the Replace statement:

Keywords = Replace(Request.Form("Keywords"), "'", "''") 
'my form value with the apostrophes filtered.
'now build the SQL code....
MySQL = "SELECT FROM MyDatabase WHERE "
MySQL = MySQL & "MyField LIKE '%" & _
Replace(Keywords, " ", "%' AND MyField LIKE '%") & "%'"

So what we did here is replace the space the user entered with an AND statement in order to make criteria that a field would have to have both keywords in it for it to be pulled. Now you could replace the AND with an OR and this way the statement will search for either of the words in the field.

About the Author, Patrick Santry

Patrick Santry, has two decades of experience in enabling businesses to take advantage of the digital landscape. A well rounded experience in technology, and business is what sets me apart from the rest of the pack. When it comes to an overall digital strategy my experience is impressive.

BS in Computer Information Systems. Four time recipient of the Microsoft MVP Award, and author of several books and magazine articles on digital technologies.


blog comments powered by Disqus

 

Thank you for visiting our site, before you leave, please visit some of the areas or information you may have missed.

Popular Articles