PDA

View Full Version : A String Line Feed???


Morgoth
11-25-2002, 09:53 PM
Ok, I have a basic problem but I have no place to begin, I am lost and wonder on how common this problem is and could be.

In a News section on my fourm. I add a String that has breaks in the lines but I don't know how I would be able to find out what is a break and replace it with an html <br> tag so it is shown on my site alright, and not in one line over and over with no paragraphs.

Anyone know how to find a Line Feed in a string?

I am still looking.
And I am about to look on google and some ASP sites.

Phip
11-25-2002, 11:42 PM
<%
Function basicClean(str_clean)
str_clean = Replace(str_clean, "<", "&lt;")
str_clean = Replace(str_clean, ">", "&gt;")
str_clean = Trim(str_clean)
str_clean = Replace(str_clean , "'", "&rsquo;")
str_clean = Replace(str_clean, chr(13), "<br />")

basicClean = str_clean
End Function
%>

this does the trick if i understand what you mean.

Replace(string, chr(13), "<br />")

Morgoth
11-26-2002, 12:41 AM
Ahh thank you Phip!

But if you don't mind, what is " Trim() ", what does it do?

Mhtml
11-26-2002, 12:50 AM
I encountered the same problem with my forum and solved it in a simliar way.

I have created what I like to call Mcode which is a large library if you will of functions which will replace defferent code strings like the ubb code on this forum eg ; [code]
So I can throw around my strings to a few functions and it breaks them down into Mcode and stores it in the database and then when it is taken out of the database it will put it back togetherr using html and ansi codes..

You will more than likely come across something like this again so you should possibly take the same approach..

PS;

A news section hey....nice Idea.

Morgoth
11-26-2002, 01:02 AM
I call them safe tags
But either way:
Thanks to Phip!
I have added one more.

Take a look on what I have so far.
Other Tags are going to be harder to do, and I think this method just isnt going to work for something like a [ url][ /ur] Because I need to store the value of the link in another string and use it twice, and it's going to also be harder to make [ url=] [/ur] as well. But I am working on that thing later.


Function SafeTags(ByVal String1)
Dim intPos, IntTag
Dim LeftString, RightString, String2, String3
For intTag = 0 to 11
Select Case IntTag
Case 0
String2 = "'"
String3 = "'"
Case 2
String2 = "<"
String3 = "<"
Case 3
String2 = ">"
String3 = ">"
Case 4
String2 = chr(13)
String3 = "<br>"
Case 5
String2 = ""
String3 = "<b>"
Case 6
String2 = ""
String3 = "</b>"
Case 7
String2 = ""
String3 = "<i>"
Case 8
String2 = ""
String3 = "</i>"
Case 9
String2 = ""
String3 = "<u>"
Case 10
String2 = ""
String3 = "</u>"
Case 11
String2 = ""
String3 = "<img src="
Case 12
String2 = ""
String3 = ">"
End Select
Do Until InStr(1, LCase(String1), LCase(String2)) = 0
intPos = InStr(1, LCase(String1), LCase(String2))
LeftString = Left(String1, intPos - 1)
RightString = Right(String1, Len(String1) - Len(String2) - Len(LeftString))
String1 = LeftString & String3 & RightString
Loop
Next
SafeTags = String1
End Function


I could add the Cases to a Database but I think if I do it this way I have more control. But I have used the microsoft made replace function for another program I made, that uses a database.

Morgoth
11-26-2002, 01:03 AM
Between Case 5 and Case 10, the UBB code used there messed up, but you can guess what was there.

Phip
11-26-2002, 06:46 AM
<%
Function cleanString(string_clean)
string_clean = Replace(string_clean, "<", "&lt;")
string_clean = Replace(string_clean, ">", "&gt;")
string_clean = Replace(string_clean, "", " target=_blank >link</a>]")
string_clean = Replace(string_clean, "", " border=0>")
string_clean = Replace(string_clean, "[ b ]", "<b>")
string_clean = Replace(string_clean, "[/ b ]", "</b>")
string_clean = Replace(string_clean, "[ i ]", "<i>")
string_clean = Replace(string_clean, "[/ i ]", "</i>")
string_clean = Replace(string_clean, "[ u ]", "<u>")
string_clean = Replace(string_clean, "[/ u ]", "</u>")
string_clean =
Replace(string_clean, "'", "&rsquo;")
string_clean = Trim(string_clean)
string_clean = Replace(string_clean, chr(13), "<br />")

cleanString = string_clean
End Function

Function basicClean(str_clean)
str_clean = Replace(str_clean, "<", "&lt;")
str_clean = Replace(str_clean, ">", "&gt;")
str_clean = Trim(str_clean)
string_clean = Replace(string_clean, "'", "&rsquo;")
str_clean = Replace(str_clean, chr(13), "<br />")

basicClean = str_clean
End Function
%>

random stuff that may be a help to you. I added spaces in some tags so vB code wont mess it up

ok i give up, vB code messed up and i can't help it.

Mhtml
11-26-2002, 09:48 AM
Morgoth, you asked what trim() was didn't you?

Hope you did coz I'm going to tell you. lol :)

Trim will get rid of the leading and trailing spaces.
So as an example using trim on a string
" THIS IS THE STRING "
will turn it into
"THIS IS THE STRING"

You can also use Ltrim and Rtrim which trim the left and right spaces depending on which you are using.

Syntax:

LTrim(string)
RTrim(string)
Trim(string)

Morgoth
11-26-2002, 01:14 PM
Mhtml,
Thank You.

I figured that's what it was.

Morgoth
11-26-2002, 01:18 PM
Originally posted by Phip

<%
Function cleanString(string_clean)
string_clean = Replace(string_clean, "<", "&lt;")
string_clean = Replace(string_clean, ">", "&gt;")
string_clean = Replace(string_clean, "", " target=_blank >link</a>]")
string_clean = Replace(string_clean, "", " border=0>")
string_clean = Replace(string_clean, "[ b ]", "<b>")
string_clean = Replace(string_clean, "[/ b ]", "</b>")
string_clean = Replace(string_clean, "[ i ]", "<i>")
string_clean = Replace(string_clean, "[/ i ]", "</i>")
string_clean = Replace(string_clean, "[ u ]", "<u>")
string_clean = Replace(string_clean, "[/ u ]", "</u>")
string_clean =
Replace(string_clean, "'", "&rsquo;")
string_clean = Trim(string_clean)
string_clean = Replace(string_clean, chr(13), "<br />")

cleanString = string_clean
End Function

Function basicClean(str_clean)
str_clean = Replace(str_clean, "<", "&lt;")
str_clean = Replace(str_clean, ">", "&gt;")
str_clean = Trim(str_clean)
string_clean = Replace(string_clean, "'", "&rsquo;")
str_clean = Replace(str_clean, chr(13), "<br />")

basicClean = str_clean
End Function
%>

random stuff that may be a help to you. I added spaces in some tags so vB code wont mess it up

ok i give up, vB code messed up and i can't help it.


I see what you did, but I think in my experiences it is best to not put functions inside of functions, that is why I used the other code.

Phip
11-26-2002, 04:21 PM
could you share some of those experiences. i've never had a problem with it.

Morgoth
11-27-2002, 12:29 AM
Well, it's not much as problems as they were conflicts. I can work more code into a replace function I made then I could, just adding it in another function. The fact you are using a function in a function is almost silly.

Your code works, and will continue to work, but I am able to edit my code if I need to. ;)

I can make it so it will not change the first word that needs to be replaced with a simple If statement, but when you use this premade function, you are unable to do that.

whammy
11-27-2002, 12:36 AM
Ok, first of all that's way too complicated for just replacing line breaks... that's as simple as:

Replace(str,vbCrLf,"<br />")

And that's it.

Morgoth, all Trim() does is delete trailing and leading spaces in a string. There are also many many other useful string manipulation functions in VBScript that you should absolutely know, i.e. RTrim(), LTrim(), Mid(), Replace(), InStr(), etc. etc... I would check out "String Manipulation in ASP" or something on google for a good tutorial. :)

And putting calls to functions inside of other functions shouldn't be a problem at all. :D

P.S. Phip, most of what you're doing in your "basicClean" function is absolutely unnecessary, since Server.HTMLEncode(string) will do it for you (i.e. &lt; etc.). Check it out. :)

P.P.S. target="whatever" is deprecated in xhtml, so you might want to look into "javascript:void window.open('link')" (for now, until they decide what to do about opening links in new windows in xhtml) if you care about xhtml, that is ;)...

As far as inserting stuff like into a database and then parsing it out how you want, that's all well and good, and I do that for some custom applications...

Lastly (to end my short story here, lol), I don't know everything there is to know about ASP, .NET, and server-side languages, but I use ASP every day (and have been developing in it for a year and a half every day) and I do like to prevent people from having to learn through trial and error what I've learned through experience. So don't take anything I said above as derogatory, just take it as constructive criticism. :)

Morgoth
11-27-2002, 01:04 AM
Thank you whammy.

whammy
11-27-2002, 01:10 AM
No problem... :)

By the way, I am continually updating my functions here (http://www.solidscripts.com/regex.txt) (which coincidentally contains the VbCrLfToBreak() function which you needed when you posted this thread!), not just for me, but for whoever might find them useful.

Ignore the NullToSpace() function right now though, as I just found a flaw in it today and replaced it with another function that works, but haven't uploaded the new version yet. ;)

P.S. Expect this to stagnate once I start dealing with .NET classes, though. :)

They seem much more exciting.