PHP nl2br() Function

PHP nl2br() Function

The nl2br() function in PHP inserts HTML line breaks before all newlines in a string.

Syntax

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

Parameter

sting(required)- This parameter specifies the input string.

is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks or not.. The default value is true.

Return

This function returns the altered string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

Example 1

 or 
) in front of each newline (\n) in a string. echo "\nAfter using the 'nl2br()' function...\nNew Sring: ".nl2br($str);  ?> 

Output

String: Hello
Example
and
Tutorial
After using the 'nl2br()' function... 
New Sring Hello
Example
and
Tutorial 

Example 2

 or 
) in front of each newline (\n) in a string. echo "\nAfter using the 'nl2br()' function...\nNew Sring: ".nl2br($str);  ?> 

Output

String: This
is
a
nl2br() example
After using the 'nl2br()' function...
New Sring: This
is
a
nl2br() example 

Example 3

 or 
) in front of each newline (\n) in a string. echo "\nAfter the nl2br() function...\n"; echo nl2br($str,false); // passed false will return
instead of
?>

Output

String: This is a
nl2br() example
After the nl2br() function...
This is a<;br>
nl2br() example