Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • nl2br

    Posted on July 4th, 2009 RubyLove 2 comments

    The nl2br() function inserts HTML <br> tags before any new lines in a string.

    PHP

    echo nl2br("foo has a\n bar");
    /* 
    foo has a<br>
    bar
    */

    Ruby

    p "foo has a\n bar".gsub("\n", "<br>\n")
    # foo has a<br>
    # bar
     

    2 responses to “nl2br”

    1. Your solution does not work.
      I always get <br> ….

    2. the point of this function is to create
      tags - when rendered as HTML, these will be line breaks.

    Leave a reply