This function converts line breaks (\n) in a string to the HTML tag <br>.
| Parameter | Data type | Default value | Note |
|---|---|---|---|
$string |
string |
not | Input string. |
$is_xhtml |
bool |
null |
Switches the escaping method according to the context. |
$retezec = 'textnext textand something else';echo nl2br($retezec);
Returns:
text<br>more text<br>and something else
Converts broken lines in text to html tags. This tag is used in places where the user enters any text (textarea) and there is a risk of using multiple lines of text.
The treatment of classic inputs (type="text") is meaningless, as multi-line text cannot be entered here.
Note: As of PHP 4.0.5, the nl2br() XHTML function is eligible. All versions before 4.0.5 will return a string with a tag inserted before the line breaks instead of <br />.
echo nl2br("Welcome\r\nThis is my HTML document", false);
Returns:
Welcome<br>This is my HTML document
string
Returns an edited string including HTML tags.
| Version | Note |
|---|---|
| 5.3.0 | Added optional is_xhtml parameter. |
| 4.0.5 | nl2br () is now XHTML compatible. All earlier versions will return a string with line breaks inserted instead of <br />. |
[Official nl2br function documentation](Official nl2br() documentation)
Jan Barášek Více o autorovi
Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.
Rád vám pomůžu:
Články a novinky nejen ze světa PHP a programování. Nenechte si ujít jediný článek.
Články píše Jan Barášek © 2009-2025 | Kontakt | Mapa webu
Status | Aktualizováno: ... | en