Forum Moderators: coopster

Message Too Old, No Replies

PHP Line Breaks not showing

neither \n nor nl2br ( ) shows any line breaks

         

benni_203

4:23 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



Hello,

I am starting to learn php and I am alraedy struggeling with the basics. My PHP program is not showing any Line Breaks :-( I wrote a program "test.php". Instead of doing line breaks, he is doing everything in one line. I tried it on my local Windows machine with Apache installed as well as on my Webhoster's server.

<?php
echo " line 1 \n line 2 \n line 3";
echo nl2br ("line A");
echo nl2br ("line B");
echo nl2br ("line B");
?>

I start to feel stupid. Anybody an idea?

Thanks,
Benni

StupidScript

5:36 pm on Jun 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Benni,

No stupidity here, just learning. :)

Maybe this example will help:

<?php

$stringVariable = "line A \n line B \n line C";

echo nl2br($stringVariable);

?>

nl2br
replaces "newline" code (
\n
or the Windows equivalent
\r\n
) with an HTML "linebreak" (
<br>
). You were asking the
nl2br
function to replace newlines in a string that didn't have any!

You wrote:

nl2br("line A");

You can see the difference here:

nl2br("line A \n line B");

In the second example, there is a newline to replace, so

nl2br
will have something to do.

Hope that helps!

benni_203

2:12 am on Jun 4, 2005 (gmt 0)

10+ Year Member



cool. Thank You! That makes sense.

I was thinking that "my method" should work because I used it in a simple PHP-Script sending an e-mail (which I got from the Internet and adjusted to my needs). There it worked ...

Thanks Again!
Benni

anshul

6:55 am on Jun 4, 2005 (gmt 0)

10+ Year Member



And you need use <form enctype="multipart/form-data" .....>
if you are using FORM, before you do nl2br($_POST['message']);