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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format string with vbcrlf to HTML <br> 1

Status
Not open for further replies.

MagnusFox

IS-IT--Management
Jan 12, 2001
51
US
First off, special thanks to Tek-Tips user jfriestman for the following code I wish to present as the basis for my question. His code take a string read from a DB and writes it to the screen by converting all of the line feeds to <br> tags. Very nice.

I want to add to the code by having the function remove any trailing <br> tags at the end of the string. This could be caused by a user adding extra line feeds in a <textarea> tag in a form.

Thanks jfriestman:
<%
Function PrepForHtml (strDirty)
Dim strClean
If IsNull(strDirty) OR Trim(strDirty) = &quot;&quot; Then
PrepForHTML = &quot;&quot;
Exit Function
End If
strClean = Trim(strDirty)
strClean = Replace(strClean, &quot;<&quot;, &quot;<&quot;, 1, -1 , 1)
strClean = Replace(strClean, &quot;>&quot;, &quot;>&quot;, 1, -1 , 1)
strClean = Replace(strClean, Chr(10), &quot;<br>&quot;, 1, -1, 1)
PrepForHtml = strClean
End Function
%>

How to use?

<%
Dim sText
sText = PrepForHTML(Rs.Fields(&quot;myField&quot;).Value)
%>

This function would still return <br> tags at the end if the DB had them in there. So the question is:

How can one remove the trailing <br> chars within the string?

TRIM() does a nice job with the blank space char, so can someone create trimBR() for us here?

Many thanks,
Magnus
 
Function PrepForHtml (strDirty)
Dim strClean
If IsNull(strDirty) OR Trim(strDirty) = &quot;&quot; Then
PrepForHTML = &quot;&quot;
Exit Function
End If
strClean = Trim(strDirty)
strClean = Replace(strClean, &quot;<&quot;, &quot;<&quot;, 1, -1 , 1)
strClean = Replace(strClean, &quot;>&quot;, &quot;>&quot;, 1, -1 , 1)
strClean = Replace(strClean, Chr(10), &quot;<br>&quot;, 1, -1, 1)
do until right(lcase(strClean),4) <> &quot;<br>&quot;
strClean = left(strClean,len(strClean)-4)
loop
PrepForHtml = strClean
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top