Forum Moderators: coopster

Message Too Old, No Replies

preserving line breaks from textarea

         

sebbothebutcher

1:29 pm on May 29, 2004 (gmt 0)

10+ Year Member



hi! i have a textfield called "note" in a form which uses POST to give it's input to another file. now here's my question: how can i preserve line breaks from the input? the next document uses

$note = $_POST["note"];

but it puts everything into a single line! is there a simple way to tell my script that it should keep the format the user intended?
thanks in advance for your fast help!

jamie

2:26 pm on May 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi sebbo,

nl2br() should do the trick

cheers

sebbothebutcher

2:38 pm on May 29, 2004 (gmt 0)

10+ Year Member



doesn't nl2br() only convert "\n" to "<br>"?
but if i type something into a textarea and write somethin into a new line, it won't write \n into the variable, or am i wrong?
thanks

venelin13

3:03 pm on May 29, 2004 (gmt 0)

10+ Year Member



it won't write \n into the variable, or am i wrong?

You will not see the \n symbol at the code, but it will be there!

sebbothebutcher

8:58 pm on May 29, 2004 (gmt 0)

10+ Year Member



hmm... i've tried to do it, but it somehow doesn't
write any <br> tags.... anybody knows, why?

corz

5:17 am on May 30, 2004 (gmt 0)

10+ Year Member



web browsers, by default, produce DOS linefeeds in the post data from textarea inputs, which is \r\n

\r = Mac carriage return
\n = Unix linefeed
\r\n = Windows/DOS LineBreak

in other words, your $_POST array will contain both mac and unix linefeeds. If they got sent, they will be there at the receiving end of the $_POST, too, syat. The trouble probably lies in what's happening to the string at one end or the other, in the scripts, somehow stripping the linebreaks off. some code?

to find out which end the trouble's at, begin (the script) by checking the incoming $_POST array with

$string = print_r($_POST, true);
, send that to a text file [webmasterworld.com], open in any decent text editor and you'll see if there's linebreaks. or echo it out between some
<pre></pre>
tags perhaps..

echo '<pre>',print_r($_POST),'</pre>';

;o)
(or

sebbothebutcher

10:19 am on May 30, 2004 (gmt 0)

10+ Year Member



hmm... the output looks like this:

Array
(
[user] => tester
[note] => this is a
new line
[submit] => Submit
)

new line is written into a new line indeed... but nl2br() doesn't convert anything into <br>... any suggestions?
if it helps: the code written into the file looks like this:

<div class="message" align="left"><h5>ID: 9 ¦ tester 30.05.04, 12:14:05</h5>
<hr/><p>
this is a

new line
</p>
</div>

corz

3:33 am on May 31, 2004 (gmt 0)

10+ Year Member



if you want to retain the look of carriage returns in an HTML document you either need to put the text between <pre></pre> tags, or convert the linefeeds to <br> tags. both are trivially simple, so I won't go into the code much. something like the always-annoyingly-backwards str_replace..

str_replace("\r",'<br>',$string);

would do the trick, allowing you to keep "real" linebreaks (unix) and <br> tags too.

;o)
(or

sebbothebutcher

10:27 pm on May 31, 2004 (gmt 0)

10+ Year Member



okay everything works now! no need to help me anymore! i've overlooked a change in the variable names! sorry to bother you!