PHP nl2br() Function
Example
Insert a line break before newlines (\n) in a string:
The browser output of the above code is as follows:
One line.
Another line.
The HTML input of the above code is as follows (view source):
One line.
Another line.
Run Example »
Definition and Usage
The nl2br() function inserts an HTML line break (
or
).
Syntax
nl2br(
string,xhtml
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| *xhtml * | Optional. A boolean indicating whether to use XHTML-compatible line breaks:
|
Technical Details
| Return Value: | Returns the converted string. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | Before PHP 4.0.5, this function inserted
. After PHP 4.0.5, this function inserts XHTML-compatible . |
在 PHP 5.3 中,新增了 *xhtml* 参数。 |
More examples
Example 1
Using the xhtml parameter, insert a line break before newlines (\n):
The browser output of the above code is as follows:
One line.
Another line.
The HTML input of the above code is as follows (view source):
One line.
Another line.
Run Example »
PHP String Reference Manual