PHP Tutorial - PHP nl2br() Function






Definition

The nl2br function inserts a HTML line break (<br/>) before all new line characters. It does not replace the line breaks which means the \n breaks are left intact.

Syntax

PHP nl2br() Function has the following syntax.

string nl2br ( string str )

Parameter

str is the string to convert.

Return

PHP nl2br() Function returns the string with new line character being converted by <br>.

Example

Convert new line character with <br>.


<?PHP
$mystr = "This is a test\nfrom java2s.com."; 
$brstr = nl2br($mystr); 
print $brstr;
?>

The code above generates the following result.