array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'function.nl2br.php', 1 => 'nl2br', ), 'up' => array ( 0 => 'ref.strings.php', 1 => 'Funções de String', ), 'prev' => array ( 0 => 'function.nl-langinfo.php', 1 => 'nl_langinfo', ), 'next' => array ( 0 => 'function.number-format.php', 1 => 'number_format', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/strings/functions/nl2br.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(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 />