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.
Ba953f4c620c9cf958d26c20e48a5e95?d=identicon&s=25 hieg (Guest)
on 2006-04-13 19: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?
0a5d6fa912dda67f9b81017fbd9a81f2?d=identicon&s=25 Shane Sherman (Guest)
on 2006-04-13 19:27
(Received via mailing list)
simple_format("whatever\n\n") => "whatever<br/><br/>"
found in TextHelper
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (Guest)
on 2006-04-13 19:30
(Received via mailing list)

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

You also might want to look at Textile
( or Markdown
Ba953f4c620c9cf958d26c20e48a5e95?d=identicon&s=25 hieg (Guest)
on 2006-04-13 19:31
Awsomr, thanks to both of you!
F239c904079c2e878f084747486177b9?d=identicon&s=25 Nicholas P. Mueller (Guest)
on 2006-04-13 20: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.