nl2br

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
bar */

Ruby

p "foo has a\n bar".gsub("\n", "
\n") # foo has a
# bar
By | 2009-07-02T15:21:51+00:00 July 4th, 2009|Categories: PHP, Ruby|Tags: , , , , , |2 Comments

About the Author:

2 Comments

  1. Jim February 3, 2011 at 10:04 am

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

  2. RubyLove February 3, 2011 at 12:49 pm

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

Comments are closed.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.