nl2br

(PHP 4, PHP 5, PHP 7, PHP 8)

nl2brInserts HTML line breaks before all newlines in a string

Descrição

nl2br(string$string, bool$use_xhtml = true): string

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

Parâmetros

string

The input string.

use_xhtml

Whether to use XHTML compatible line breaks or not.

Valor Retornado

Returns the altered string.

Exemplos

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 />

Veja Também

  • htmlspecialchars() - Convert special characters to HTML entities
  • htmlentities() - Convert all applicable characters to HTML entities
  • wordwrap() - Quebra uma string em um dado número de caracteres
  • str_replace() - Substitui todas as ocorrências da string de pesquisa com a string de substituição
To Top