(PHP 4, PHP 5, PHP 7, PHP 8)
nl2br — Inserts HTML line breaks before all newlines in a string
Returns string
with <br />
or <br>
inserted before all newlines (\r\n
, \n\r
, \n
and \r
).
string
The input string.
use_xhtml
Whether to use XHTML compatible line breaks or not.
Returns the altered string.
Exemplo #1 Using nl2br()
<?php
echo nl2br("foo isn't\n bar");
?>
O exemplo acima produzirá:
foo isn't<br /> bar
Exemplo #2 Generating valid HTML markup using the use_xhtml
parameter
<?php
echo nl2br("Welcome\r\nThis is my HTML document", false);
?>
O exemplo acima produzirá:
Welcome<br> This is my HTML document
Exemplo #3 Various newline separators
<?php
$string = "This\r\nis\n\ra\nstring\r";
echo nl2br($string);
?>
O exemplo acima produzirá:
This<br /> is<br /> a<br /> string<br />