php :: new line in textarea?
How do you create a new li开发者_如何学编程ne in a textarea when inserting the text via php?
I thought it was \n
but that gets literally printed in the textarea.
Thanks
Without seeing your code I cannot be sure, but my guess is you are using single quotes ('\n') instead of double quotes ("\n").
PHP will only evaluate escape sequences if the string is enclosed in double quotes. If you use '\n', PHP will just take that as a literal string. If you use "\n", PHP will parse the string for variables and escape sequences and print a new line like you are expecting.
Try
$text = 'text line one' . PHP_EOL . 'text line two';
echo '<textarea>' . $text . '</textarea>';
Will add each text on reparate line in texarea.
PHP Side: from Textarea string to PHP string
$newList = ereg_replace( "\n",'|', $_POST['theTextareaContents']);
PHP Side: PHP string back to TextArea string:
$list = str_replace('|', ' ', $r['db_field_name']);
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
Carriage Return
\n
\r
<br />
^M
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.
i have used \p
for text files. try
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
.
I had a similar issue and the solution to mine was double-escaping the newline character. \\n
use nl2br()
. The nl2br
convert \n
to <br />
as string .but <br />
(string) not create New line. you should repalce <br />
(string) to <br>
then this will work.
example:
$content = "foo \n bar";
echo str_replace("<br />","<br>",nl2br($content));
result:
foo
bar
split by line:
function SplitLine($txt){
$txt = nl2br($txt);
$txt = explode("<br />",$txt);
return $txt;
}
I sent
foo
bar
with textarea
as POST
method to php.
print_r(SplitLine($_POST['text_area']));
result:
Array ( [0] => foo [1] => bar)
you can use for
loop to get data and print it:
$result = SplitLine($_POST['text_area']);
for($i = 0;$i < count($result);$i++){
echo $result[$i]."<br>";
}
and result is
foo
bar
full code (Only For TEST):
<?php
//MyFunction
function SplitLine($txt){
$txt = nl2br($txt);
$txt = explode("<br />",$txt);
for($i = 0;$i < count($txt);$i++){
echo $txt[$i]."<br>";
}
return $txt;
}
//My Code
if(isset($_POST['submit_data'])){
print_r(SplitLine($_POST['text_area']));
}
?>
<br>
<form method='post'>
<textarea name='text_area'></textarea>
<br>
<input type='submit' value='Send Data :)' name='submit_data' />
</form>
$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']);
Use like this for dynamically enter each line you can use
echo chr(13)
精彩评论