php :: new line in textarea?

后端 未结 10 1981
暖寄归人
暖寄归人 2020-12-09 09:02

How do you create a new line in a textarea when inserting the text via php?

I thought it was \\n but that gets literally printed in the textarea.

<
相关标签:
10条回答
  • 2020-12-09 09:27

    Use like this for dynamically enter each line you can use

    echo chr(13)
    
    0 讨论(0)
  • 2020-12-09 09:31

    Just to complete the solution for that issue, as others mentioned use "\n" *(or in some cases "\r\n") instead of '\n'

    And when you want to show the textarea contents using php and keeping new lines, you need to use nl2br, so if you assing a varialbe for your text and call it $YOUR_TEXT , you should use that function as: nl2br($YOUR_TEXT)

    More details could be found here


    • Unix and Unix programs usually only needs a new line \n, while Windows and Windows programs usually need \r\n.
    0 讨论(0)
  • 2020-12-09 09:35

    2020 - Google brought me here for something similar.

    Building on answer from user @Va1iant above

    Environment:

    • HTML 5 / Bootstrap 4.5
    • php 7.3.x
    • mariadb 10.2.x

    Problem :

    • Input Form with textarea ( no issue of \r\n appearing in database after posting )
    • Edit form with textarea ( \r\n appears in db and also in the edit form after posting )

    Solution:

    • DB - column type for the textarea content set to 'varchar'
    • Edit Form - code snippet for content inside textarea( stripcslashes used ) shown below
    <?= isset($_POST['MailingAddress']) ? stripcslashes(misc::esc($_POST['MailingAddress'])) : misc::esc($data['current_mailing_address']) ; ?>
    
    

    where misc::esc -> return htmlspecialchars($var, ENT_QUOTES|ENT_HTML5, 'UTF-8');

    Note :

    I did not experiment with white-space: pre-line; as outlined at CSS Tricks so that could be a solution too - without using stripcslashes.

    0 讨论(0)
  • 2020-12-09 09:36

    Try

    $text = 'text line one' . PHP_EOL . 'text line two';
    echo '<textarea>' . $text . '</textarea>';
    

    Will add each text on reparate line in texarea.

    0 讨论(0)
  • 2020-12-09 09:37

    What Alay Geleynse said was right, I had the same problem as you and the issue was due to the escape characters (\r, \n) was there. To 'unescaped' the variable I used $var = stripcslashes($var) and it's shown correctly

    0 讨论(0)
  • 2020-12-09 09:43
    $row['content']=stripslashes($row['content']);
    $row['content']=str_replace('<br />',"newline",$row['content']);
    $row['content']=htmlentities($row['content']);
    $row['content']=str_replace('newline',"<br>",$row['content']);
    
    0 讨论(0)
提交回复
热议问题