# Equivilent to PHP's nl2br?

**URL:** <https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760>\
**Category:** Rails\
**Created:** [April 13, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760 "2006-04-13T19:16:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@hieg](https://www.ruby-forum.com/u/hieg)\
**Post date:** [April 13, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760/1 "2006-04-13T19:16:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hieg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@hieg](https://www.ruby-forum.com/u/hieg)\
**Post date:** [April 13, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760/2 "2006-04-13T19:27:09Z")

</div>

simple\_format(“whatever\n\n”) =\> “whatever  
  
”  
found in TextHelper

---

<div class="post-metadata">

**Author:** ![hieg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@hieg](https://www.ruby-forum.com/u/hieg)\
**Post date:** [April 13, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760/3 "2006-04-13T19:30:15Z")

</div>

Try

some\_string.gsub!(/\n/, ‘  
’)

You also might want to look at Textile  
([Textism: Tools: Textile](http://www.textism.com/tools/textile/index.php)) or Markdown

---

<div class="post-metadata">

**Author:** ![hieg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@hieg](https://www.ruby-forum.com/u/hieg)\
**Post date:** [April 13, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760/4 "2006-04-13T19:31:47Z")

</div>

Awsomr, thanks to both of you!

---

<div class="post-metadata">

**Author:** ![hieg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@hieg](https://www.ruby-forum.com/u/hieg)\
**Post date:** [April 13, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/equivilent-to-phps-nl2br/57760/5 "2006-04-13T20:01:05Z")

</div>

I wrote a helper function:

def nl2br(string)  
string.gsub("\n\r","  
").gsub("\r", “”).gsub("\n", “  
”)  
end

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  
(@object.text))

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

Nicholas P. Mueller
