SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,768
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    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.

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    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

  5. #5
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That is correct. HTML does not render newlines.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,768
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    Did I get that right?!
    Pretty much. Test it by echoing out some PHP code.

  7. #7
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    Pretty much. Test it by echoing out some PHP code.
    I did some testing but am confused?!

    Here is my test code...
    PHP 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 \n 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

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •