SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nl2br equivalent

    Is there a javascript equivalent of PHP's nl2br() function? I have to send the value of a textarea to a new window that will display the text of what was in the box, which is why I need to somehow convert newlines to breaks.

    Thanks,
    Greg

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    There's nothing built-in, but you can write your own nl2br function:
    Code:
    function nl2br(someText) {
      return someText.replace("\n", "<br />");
    }

  3. #3
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    That seemed to work, but for the first linefeed only. I figured out the solution is to make a loop that repeats the function until the end of the textarea.

  4. #4
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gregory
    That seemed to work, but for the first linefeed only. I figured out the solution is to make a loop that repeats the function until the end of the textarea.
    A better solution than a loop would be to use a Regular Expression with the g (global) and m (multi-line) parameter:
    Code:
    function nl2br(someText) {
       	return someText.replace ( /\n/gm, '<br />' );
       }
    Of course, that's just my opinion. I could be wrong.

  5. #5
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh...that is better! Thanks, Mr. Brownstone.

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use theses
    Code:
    String.prototype.nl2br = function()
    {
    	return this.replace( /\n/g, "<br />" );
    }
    Code:
    String.prototype.br2nl = function()
    {
    	return this.replace( /\<br ?/?\>/g, "\n" );
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    CA
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, you and your prototypes. when will you ever be satisfied.
    -ChaCha

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...only when he owns the complete set
    ::: certified wild guess :::

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    ...only when he owns the complete set
    Sweet!

    Mmmm, my birthday is next month!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by beetle
    Sweet!

    Mmmm, my birthday is next month!
    I'll send you the frisbee


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
  •