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.
Use like this for dynamically enter each line you can use
echo chr(13)
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
\n
, while Windows and Windows programs usually need \r\n
.2020 - Google brought me here for something similar.
Building on answer from user @Va1iant above
Environment:
Problem :
Solution:
<?= 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.
Try
$text = 'text line one' . PHP_EOL . 'text line two';
echo '<textarea>' . $text . '</textarea>';
Will add each text on reparate line in texarea.
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
$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']);