How to echo an input of an textarea with line breaks?

前端 未结 3 785
深忆病人
深忆病人 2020-12-28 16:08

I\'m using an textarea to send text to my DB.

Screenshot of db:

\"enter

<
相关标签:
3条回答
  • 2020-12-28 16:47

    When displaying text, use nl2br() to convert newlines to <br/> tags, i.e., instead of <?php echo $row['text']; ?>, use <?php echo nl2br($row['text']); ?>.

    By default, browsers display newlines as spaces, therefore they have to be converted to <br/> tags.


    For those who find this useful - please consider using white-space: pre-line, suggested by Emil Vikström. I'm not a web guy anymore and easily can't verify this, but Boaz says in comments that it is supported by all modern browsers. If so, that should be preferred to using nl2br().

    0 讨论(0)
  • 2020-12-28 16:51

    An alternative to nl2br is to make use of the CSS attribute white-space:

    white-space: pre-line;
    
    0 讨论(0)
  • 2020-12-28 16:53

    I put as follows but not working with single quotes.

    echo $row['text'].'\n';
    

    Put the double quotes. Then worked.

    <textarea rows="10" cols="62" style="white-space: pre-line;" wrap="hard">
    <?php echo $row['text']."\n"; ?>
    </textarea>
    

    When we getting data it is comming with \r\n. Also use the double quotes there.

    0 讨论(0)
提交回复
热议问题