Echo respecting mysql line break

Asked

Viewed 822 times

1

I would like to know how to make the echo PHP respect the line break of a text in the database.

I have a text field in the registration form like this:

<textarea id="descricao" name="descricao" class="input form-control col-lg-10" rows="5" required></textarea>

In the database it looks like this:

inserir a descrição da imagem aqui

I would like that at the time of echo <?php echo $row['descricao']; ?>, the same BD line break was respected in the exhibition.

But it’s all coming up on one line:

inserir a descrição da imagem aqui

1 answer

3


Try using the function nl2br()

PHP documentation:

nl2br - Inserts HTML line breaks before all newlines in one string

Changes to the following:

<?php echo nl2br($row['descricao']); ?>
  • Cool, I didn’t know that function. I was going to have one str_replace("\n", "<br>\n", $row['descricao']).

  • It worked! just out of curiosity that I had now. To display only as text, without the interpretation of html, as it would be?

  • @Kaduamaral yes, it is an option that also works

  • @James would be something like : <?php echo strip_tags($row['descricao']) ?>

  • @13dev Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.