简体   繁体   中英

How to insert “Enter” in database

I have this situation: An user inserts a text in a textarea. The user decides when to press Enter, so the text may look like this:

text text text text
text text
text
text text 
text

text text text text

This text goes into a database table and later is echoed in a div. The problem is that when it is echoed it look like this:

text text text text text text text text text text text text text

What can I do to insert the text in the database in such a way that when is echoed it shoes the "Enters" as well ?

Use php nl2br () function

<?php
  echo nl2br("YOUR TEXTAREA VALUE HERE", false);
?>

Just fyi, nl2br is similar to the:

$details = my string with break lines given by user.

str_replace(array("\r\n", "\r", "\n"), '<br>', $details);
Or 
str_replace(array("\r\n", "\r", "\n"), '<br/>', $details);

It will catch all break lines then add the
same as the nl2br function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM