converting all newline characters to br tag

August 7, 2006 at 10:48 am 14 comments

I was surprised as there is no function in ruby to convert all newline characters to <br>.

Here is a php nl2br equivalent method to convert all newline characters (\n) to break tag (<br>) in a string.

 def nl2br(s)
     s.gsub(/\n/, '<br>')
 end

Entry filed under: rails, Ruby, RubyonRails. Tags: .

Make ids in URL Search engine friendly Tab Problem in rails .rhtml files

14 Comments Add your own

  • 1. Pratik  |  August 8, 2006 at 2:42 pm

    def nl2br(s)
    s.gsub(/\n/, ”)
    end

    This would be more appropriate :)

    Reply
  • 2. Akhil Bansal  |  August 18, 2006 at 12:30 pm

    This will remove all new line characters not replace them by <br> :-)

    Reply
  • 3. Iain  |  March 4, 2007 at 7:23 pm

    s.gsub didnt work for me alone, but

    var = s.gsub(/\n/, ”)

    did work. Just in case anyone else has the same problem

    Reply
  • 4. Marcio Lima  |  August 12, 2007 at 5:26 am

    i`m using

    param.gsub(/\n/, ”)

    work nice

    Reply
  • 5. Kalle  |  August 20, 2007 at 8:34 pm

    Actually… s.gsub(/\n/, ”) vorked like a charm! Thanks man. ;o)

    Reply
  • 6. AllForYou123  |  August 30, 2007 at 4:39 am

    Helo, it is very interesting site. If You want you can visit mine. chain hang jibbs low lyric I have make it myself. There you can find all about chain hang jibbs low lyric etc…

    Reply
  • 7. Idetrorce  |  December 15, 2007 at 12:18 pm

    very interesting, but I don’t agree with you
    Idetrorce

    Reply
  • 8. Wiras Adi  |  January 15, 2008 at 11:17 am

    Don’t you think that it should be something like this:

    def nl2br(s)
    s.gsub(/(\r)?\n/, “”)
    end

    Windows has different way of breaking a line, that code will check also for the carriage return Windows used.

    Reply
  • 9. Wiras Adi  |  January 15, 2008 at 11:19 am

    Sorry, forgot to escape the br tag.

    def nl2br(s)
    s.gsub(/(\r)?\n/, “<br />”)
    end

    Reply
  • 10. Akhil Bansal  |  January 15, 2008 at 11:27 am

    I don’t use windows so I don’t have that idea

    Reply
  • 11. Alen  |  April 5, 2008 at 12:35 pm

    Don’t understand why Ruby as a multi-purpose language directly should have such function/method.

    If you using Rails web framework, there is a simple_format() method of ActionView::Helpers::TextHelper module that does exactly that and more.

    Reply
  • 12. ian  |  June 2, 2008 at 12:51 pm

    if you are using rails you can use the simple_format function in your views.

    here’s the source code from the api (http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M001057)

    def simple_format(text)
    content_tag ‘p’, text.to_s.
    gsub(/\r\n?/, “\n”).
    gsub(/\n\n+/, “\n\n”).
    gsub(/([^\n]\n)(?=[^\n])/, ‘\1’)
    end

    Reply
  • 13. Alessandro DS  |  May 30, 2010 at 3:23 pm

    You can use Textile !

    Reply
  • 14. Allan  |  October 18, 2013 at 4:17 am

    Hello there, I
    am new to running a blog and internet sites in geheral and was
    wanting to know how you got the “www” included in your
    domain name? I see youhr domain name,
    “https://webonrails.wordpress.com/2006/08/07/converting-all-newline-characters-to/”
    has the www and my domain looks like, “http://
    mydomain.com”. Do you know exactly how I can change this?
    I’m using
    Wordpress. Thanks

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Important Notice…

I have moved this blog to http://webonrails.com . I will continue posting at http://webonrails.com, not on https://webonrails.wordpress.com.

Kindly update your book marks to http://webonrails.com

and http://webonrails.com/feed .

I am really very sorry for inconvenience.

Comming Soon….

Domain for Sale

TheRailsHosting.com


(Contact me)

Recent Posts

Calender

August 2006
M T W T F S S
« Jul   Sep »
 123456
78910111213
14151617181920
21222324252627
28293031  

Subscribe

  • Add to Google
  • Subscribe in NewsGator Online
  • Subscribe with Bloglines
  • WebOnRails on feedburner
  • technorati

del.icio.us

Feeds

Flickr Photos

A bellezza di a natura (C☺rsica)

JWL5932 Mountain Hare..

Black and Gold

More Photos

Visitor’s Location

Blog Stats

  • 17,978 hits

%d bloggers like this: