Purpose of nl2br

I looked in the PHP Manual about nl2br but I am not understanding why you would want to use it…

Can someone help me understand things?!

Debbie

It is for if you wanted to preserve new lines in HTML. Thats all.
With HTML new lines, multiple spaces are ignored and condensed in a single space.
Changing new lines to “<br>” tags preserves them.

nl2br() converts line breaks submitted by the browser into <br /> tags so it will be rendered as new lines in the browser when displayed. Without nl2br(), content will appear on new lines in the HTML source code but will not be rendered on new lines by the browser.

Without nl2br(), this line

and this line would be on the same line.

Look at the HTML source to see the <br /> tags create the rendered new lines. The forum code took my two newline characters submitted after each of the above lines with my post and converted them into <br /> tags which the browser is capable of rendering as new lines.

Let me try to re-phrase what you just said…

So without nl2br(), if a User typed in Comments after one of my Articles and hit the <return> button as he/she typed then those carriage returns would show up as the User typed, AND they would show up in the HTML source, BUT when the Comments were rendered by the browser everything would be together with no carriage returns.

Typing in browser…

I liked your article, but have a few suggestions…

1.) The article was too short

2.) You need more supporting information to back up your criticisms

In HTML Source…

I liked your article, but have a few suggestions…

1.) The article was too short

2.) You need more supporting information to back up your criticisms

When Comments are Displayed…

I liked your article, but have a few suggestions… 1.) The article was too short 2.) You need more supporting information to back up your criticisms

Did I get that right?! :-/

Thanks,

Debbie

That is correct. HTML does not render newlines.

Pretty much. Test it by echoing out some PHP code.

I did some testing but am confused?! :-/

Here is my test code…


<?php

if (isset($_POST['submit'])){
	//save comment do the database
}

?>
<html>
 <head></head>
 <body>
  <form method="post" action="">
    <?php if (isset($_POST['preview'])): ?>
	<div class="comment">
	  <?php //echo $_POST['comment']; ?>
	  <?php  echo nl2br((htmlentities($_POST['comment']))); ?>
	</div>
     <?php endif; ?>

     <h2>Comment</h2>
     <textarea name="comment"><?php if (isset($_POST['comment'])) echo htmlentities($_POST['comment']); ?></textarea>
     <input type="submit" name="submit" value="Submit Comment">
     <input type="submit" name="preview" value="Preview Comment">
  </form>
 </body>
</html>

Here is what I don’t understand…

If I enter in this text into the Text Area…

I liked your article, but have a few suggestions…

1.) The article was too short

2.) You need more supporting information to back up your criticisms

Why do I need nl2br() on Line 15 to keep the spaces between the lines above, but I do NOT need a nl2br() on line 20 - the code which creates a “sticky” Text Area - to maintain the spaces?!

That is, why does the Text Area recognize the
characters from the original text that was entered, but when I echo what is in $_POST[‘comments’], it is not smart enough to do that??

Debbie

TextArea preserves newlines and spaces. Its behavior is defined as part of the HTML spec.
When displayed as part of the HTML content, spaces and newlines are condensed into a single space.
Again, defined in the HTML spec.

Strange.

Okay, thanks.

Debbie