Forum: Ruby on Rails equivilent to PHP's nl2br?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
hieg (Guest)
on 2006-04-13 21:16
Hey all, this is a pretty simple question but I can't seem to find
anything on the  wikki or google... Is there a Ruby/Rails equivilent to
PHP's nl2br function?
Shane S. (Guest)
on 2006-04-13 21:27
(Received via mailing list)
simple_format("whatever\n\n") => "whatever<br/><br/>"
found in TextHelper
Daniel H. (Guest)
on 2006-04-13 21:30
(Received via mailing list)

some_string.gsub!(/\n/, '<br />')

You also might want to look at Textile
( or Markdown
hieg (Guest)
on 2006-04-13 21:31
Awsomr, thanks to both of you!
Nicholas P. Mueller (Guest)
on 2006-04-13 22:01
(Received via mailing list)
I wrote a helper function:

   def nl2br(string)
     string.gsub("\n\r","<br>").gsub("\r", "").gsub("\n", "<br />")

This seems to work fairly well.  I put it in the application helper
wherever I think I'll need it.  I feed it html, say like this: nl2br(h

You could certainly modify it so that it automatically wraps the
input in an h().

Nicholas P. Mueller
This topic is locked and can not be replied to.