PHP nl2br() Function

The nl2br() function inserts HTML line breaks (
or
) in front of each newline (\n) in a string.

string nl2br ( string $string [, bool $is_xhtml = TRUE ] )

Returns string with
or
inserted before all newlines (\r\n, \n\r, \n and \r).

Example -

Example #1 Using nl2br()

The above example will output : -

foo isn't<br />
 bar

Example #2 Generating valid HTML markup using the is_xhtml parameter

The above example will output : -

Welcome<br>
This is my HTML document

Example #3 Various newline separators

The above example will output : -

This<br />
is<br />
a<br />
string<br />

ParameterDescription
stringThe input string.
is_xhtmlWhether to use XHTML compatible line breaks or not.

Returns the altered string.