• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

converting all newline characters to br tag

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

5 Responses so far.

  1. Pratik says:

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

    This would be more appropriate :)

  2. Akhil Bansal says:

    This will remove all new line characters not replace them by &lt;br&gt; :-)

  3. Stian says:

    I believe he was reffering to the /\n/ part. Since /n/ will remove all n-characthers and replace them with :P

  4. Nadeem Yasin says:

    for development in rails, just use simple_format(str) function.
    Its really simple..


Github2S3: Backup Github Repositori...

Cross Posted from http://vinsol.com/blog Who doesn't know GitHub now a days, ...

Edge Rails: Time#current...

Returns Time.zone.now when config.time_zone is set, otherwise just returns Time.now

Edge Rails: Array#random_element...

Array#rand is deprecated in favor of Array#random_element and will be ...

Edge Rails Updates...

New edge rails deprecations: Time#last_year is deprecated in favor of Time#prev_year Time#last_month ...

Download recursive directories with...

Using ftp to download multiple files using 'mget' command is ...

Edge Rails: Time#current...

Returns Time.zone.now when config.time_zone is set, otherwise just returns Time.now

Edge Rails: Array#random_element...

Array#rand is deprecated in favor of Array#random_element and will be ...

Edge Rails Updates...

New edge rails deprecations: Time#last_year is deprecated in favor of Time#prev_year Time#last_month ...

uninitialized constant ActionMailer...

Just a quick note, If you are getting "uninitialized constant ...

11 Things to Consider Before Deploy...

Cross Posted from http://vinsol.com/blog At VinSol, we have been ...