Função PHP nl2br()
Exemplo
Insira quebras de linha onde as novas linhas (\n) ocorrem na string:
<?php
echo nl2br("One line.\nAnother line.");
?>
A saída do navegador do código acima será:
One line.
Another line.
A saída HTML do código acima será (View Source):
One line.<br />
Another line.
Definição e uso
A função nl2br() insere quebras de linha HTML (<br> ou <br />) na frente de cada nova linha (\n) em uma string.
Sintaxe
nl2br(string,xhtml)
Valores de parâmetro
Parameter | Description |
---|---|
string | Required. Specifies the string to check |
xhtml | Optional. A boolean value that indicates whether or not to use XHTML compatible line breaks:
|
Detalhes técnicos
Valor de retorno: | Retorna a string convertida |
---|---|
Versão do PHP: | 4+ |
Registro de alterações: | O parâmetro xhtml foi adicionado no PHP 5.3. Antes do PHP 4.0.5, ele inseria <br>. Após o PHP 4.0.5 ele insere <br />. |
Mais exemplos
Exemplo
Insira quebras de linha onde ocorrem novas linhas (\n), usando o parâmetro xhtml:
<?php
echo nl2br("One line.\nAnother line.",false);
?>
A saída do navegador do código acima será:
One line.
Another line.
A saída HTML do código acima será (View Source):
One line.<br>
Another line.
❮ Referência de String PHP