PDA

View Full Version : Formatting text area?!?


Tidus
11-10-2002, 06:09 AM
Hello.

I am having a problem with all my forms and stuff.

How do I make a text area in a form, similar to the one that you use to post messages on this forum. Like what I mean is, with the database that I have at the moment, if you put a line break or somethign in the text area of a form, it doesn't add it in the database.

Example: This is how I put it into the text area:-

Hello!

It's James Here.

bye

But this is how it comes out.

Hello! It'/s James Here. bye

any help?

James

firepages
11-10-2002, 07:45 AM
The tabs and newlines are almost certainly in your database, (unless you run a script that removes them first)

The issue is converting those back into HTML as opposed to '\t' '\r' & '\n' etc

You do not say which database or lannguage you are using to access it but in PHP


<?echo nl2br($row['from_database']);?>

will convert \n or \r\n to <br />(actually it adds a <br /> to the end of a string the \n is kept intact)

if that function did not exist I would be using
<?str_replace("\n","\n<br />",$row['from_database']);?>

and there are similar replace functions in most server-side languages.

as for the '\' before your apostrophe , PHP provides stripslashes() though you can str_replace them as well if required.

Tidus
11-10-2002, 07:53 AM
i am using a MySQL database - is it the same?

Spudhead
11-15-2002, 11:30 AM
What language are you using to write your web pages? PHP? ASP?

In ASP, I usually do this:

When you drop the textarea value into the database, escape() it first:

var myTextValue=escape(Request.Form("myTextArea"));

this replaces all the line breaks with "%0D%0A", so when you get the text back out of the database, you just replace it:

var myText = rs.Fields("textarea").value;
while(myText .indexOf("%0D%0A")>-1){
myText = myText .replace("%0D%0A", "<br>");
}

And then you can Response.Write(myText) and it's got line breaks in.

Any use?

whammy
11-17-2002, 12:54 AM
Spudhead, FYI - a much easier method (for ASP):


Function VbCrLfToBreak(byVal str) ''''''''''''''''
If IsNull(str) Then str = ""
VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
End Function '''''''''''''''''''''''''''''''''''''

Response.Write(VbCrLfToBreak(rs("Whatever")))


:)

whammy
11-18-2002, 04:42 AM
I forgot to mention, line breaks are preserved when you put a textarea into a database, you just have to replace them with <br /> when you retrieve them.

So escaping them when you put them in is just a waste of time. :)

soundboy
10-15-2004, 06:56 PM
Hi.. I have this same problem with a textarea in a form I made... line breaks are output as \r\n and nl2br() on the string just returns the same string, it's not working like it should for some reason. I also tried doing $str = str_replace("\r\n","<br />",$str); and this, for some reason, is not working either. I tried doing it to the string variable both before insertion to the databese, and also after retrieval, and nothing works... I keep getting the \r\n :(

whammy
10-16-2004, 02:48 AM
Can you post exactly what you retrieve from the database right now, compared to the string you input in the textarea? The answer lies there!!! I guarantee it.

soundboy
10-18-2004, 07:33 PM
ok, before I do that, I just want to ask: what's better, to replace \r\n to <br /> before insertion to the database, or insert it with the \r\n and then replacing the characters when the string is going to be displayed?

bnovc
10-18-2004, 08:30 PM
The first option would be better because it would be converted less times.

soundboy
10-18-2004, 09:01 PM
Thanks for your help, I solved the problem. It was a stupid function that was escaping special characters from my strings, turning \r\n into \\r\\n, and thus making nl2br useless. Thanks again!