Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Carriage Return

Status
Not open for further replies.

TonDeaf

Programmer
Feb 28, 2002
4
DE
How do I parse the results of a FORM to substitute <BR> for a carriage return in VBscript?
 
Do you mean VbCrLf alternatively Chr(10) & Chr(13)?
 
I have a script on my page that takes noraml form content - I assume it has the Chr(13) in it when it gets sent. It saves with the carriage return in a text file.

The text file is then imported later into an HTML file. It looks fine there, but the problem is that the linefeeds are lost because my script doesn't substitute <br> for Chr(13). So I need the code that searches the whole string and replaces <br> for Chr(13) before appending it at the end of my text file.

I tried using <pre>, but then it streches my table to infinity.

<pre width=&quot;x&quot;> is apparently not supported with IE 4/5/6. Otherwise that would have worked.

Many thanks B827!
 
use a regular expression
get the entire string out of the file using the filesystem object obj.ReadAll() method.
now setup the regexp

Dim oFSO : Set oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Dim oHTMLfile : Set oHTMLfile = oFSO.OpenTextFile(&quot;c:\MyFile.html&quot;, ForReading, False, UseTristateDefault)
Dim oRegularExpression : Set oRegularExpression = New RegExp

StringToSearch = oHTMLfile.ReadAll()

With oRegularExpression
.Pattern = &quot;Chr(13)&quot; ' or vbCrLf etc.
.IgnoreCase = True
.Global = True
End With

now replace all occurences of the vbCrLf with <BR> into string to make up a &quot;FinalString&quot; string or whatever and use

Set oHTMLfile = oFSO.OpenTextFile(&quot;c:\MyFile.html&quot;, ForWriting, False, UseTristateDefault)
oHTMLfile.Write(FinalString)

to write the result back into your file.
(send me it when you are done if you want groovypumpkin@hotmail.com).
 
Note: This question might get better answers over in the ASP forum.

&quot;stretches my table to infinity&quot; ??

You didn't say anything about a table.

Ok, so it sounds like what you have is a case where there is an HTML page with a form on it, and this form contains a textarea in which your user can enter multiple text lines, bashing the Enter key where apropriate to separate lines.

This form is submtted to an ASP (?) page where you want to store the textarea some place (file? database?). Later on you will retrieve this saved text and display it as web content on another page.

Ideally, you need to capture the &quot;newlines&quot; at the client side and change them to some universal line delimiter before sending them to the ASP page. Only a Windows client can be relied upon to use CRLF as the line delimiter. A Mac will only send CR under many circumstances, as this is the Mac's standard line delimiter. A *nix box will send only LF for the same reason. This is why VBScript has the constant vbNewLine in the first place. On a Windows machine it is the same as vbCrLf, on a *nix box it is equal to vbLf, on a Mac it equals vbCr.

Some of this may be moot - if you are assuming Windows clients you know that vbCrLf is always used.

You would pick a delimiter, say &quot;|&quot; for example. Then:
Code:
textarea1.value = Replace(textarea1.value, &quot;|&quot;, &quot;?&quot;)
textarea1.value = Replace(textarea1.value, vbNewLine, &quot;|&quot;)
The first Replace( ) is to zap any &quot;|&quot; the user types to a &quot;?&quot; (choose your own symbol here).

This code above should run upon a click of your submit button.

Then your ASP code receiving the posted data would store the data.

To retrieve the data and display it, you need to take a different action depending on whether you are going to simply display the stored text or if you are going to let the user edit the text in a new textarea.

If you want to display the text in say, a table cell, just use Replace(strStoredText, &quot;|&quot;, &quot;<BR>&quot;) before outputting it.

If you are putting it back into a textarea for further editing by the user, you need to consider the fact that in HTML textareas, content is basically <pre>-formatted: any <BR>s you put out will show as <BR> text in the textarea. So here you use Replace(strStoredText, &quot;|&quot;, vbNewLine) to output the text.

Even though vbNewLine will be a CR and a LF on your ASP machine (uless you are running something funky under *nix, like ChiliSoft ASP) it doesn't matter. All browsers will handle CR/LF as a line delimiter on output. You use vbNewLine rather than vbCrLf as documentation to indicate your intent as a developer, to any other developer who later works on your ASP pages.

Er, final note

If you have the simple case where you know you have Windows clients out there, and you know that you will only be displaying the stored text back for users to read (and not edit within a new textarea), you can simply store the result of Replace(Request.Form(&quot;textarea1&quot;), vbNewLine, &quot;<BR>&quot;) to your file or database. Then you have nothing special to do upon retrieval - you'll always send it back to the browser with <BR> line delimiters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top