PHP nl2br() Function
Example
Insert a newline (\n) before newlines (\n) in the string:
<?php echo nl2br("One line.\nAnother line."); ?>
Browser output of the above code:
One line. Another line.
HTML input of the above code (View source code):
One line.<br /> Another line.
Definition and Usage
The nl2br() function inserts HTML line breaks (<br> or <br />) before each newline (\n) in the string.
Syntax
nl2br(string,xhtml)
Parameter | Description |
---|---|
string | Required. Specifies the string to be checked. |
xhtml |
Optional. Boolean value indicating whether to use compatible XHTML line breaks:
|
Technical Details
Return Value: | Returns the converted string. |
PHP Version: | 4+ |
Update Log: |
Before PHP 4.0.5, the function inserts <br>. After PHP 4.0.5, the function inserts <br /> compatible with XHTML. Added in PHP 5.3: xhtml Parameters. |
More Examples
Example 1
By using xhtml Parameters, insert a newline (\n) before (\n):
<?php echo nl2br("One line.\nAnother line.",false); ?>
Browser output of the above code:
One line. Another line.
HTML input of the above code (View source code):
One line.<br> Another line.