nl2br()
nl2br() — Inserts HTML line breaks before all newlines in a string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).
Signature
nl2br(string $string, bool $use_xhtml = true): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $string | string | The input string. |
| $use_xhtml | bool | Whether to use XHTML compatible line breaks or not. |
Return value
Returns the altered string.
Example
A runnable snippet:
echo nl2br("foo isn't\n bar");
Output:
foo isn't<br /> bar
See also
htmlspecialchars(), htmlentities(), wordwrap(), str_replace()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page nl2br() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.