php :: textarea 中的新行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3911261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php :: new line in textarea?
提问by Hailwood
How do you create a new line in a textarea when inserting the text via php?
通过php插入文本时如何在textarea中创建新行?
I thought it was \n
but that gets literally printed in the textarea.
我以为是,\n
但实际上是在 textarea 中打印出来的。
Thanks
谢谢
回答by Alan Geleynse
Without seeing your code I cannot be sure, but my guess is you are using single quotes ('\n') instead of double quotes ("\n").
没有看到您的代码,我无法确定,但我的猜测是您使用的是单引号 ('\n') 而不是双引号 ("\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.
如果字符串用双引号括起来,PHP 只会评估转义序列。如果您使用 '\n',PHP 只会将其视为文字字符串。如果您使用“\n”,PHP 将解析变量和转义序列的字符串,并像您期望的那样打印一个新行。
回答by OzzyCzech
Try
尝试
$text = 'text line one' . PHP_EOL . 'text line two';
echo '<textarea>' . $text . '</textarea>';
Will add each text on reparate line in texarea.
将在 texarea 的修复行上添加每个文本。
回答by cssyphus
PHP Side: from Textarea string to PHP string
PHP 端:从 Textarea 字符串到 PHP 字符串
$newList = ereg_replace( "\n",'|', $_POST['theTextareaContents']);
PHP Side: PHP string back to TextArea string:
PHP 端:PHP 字符串回 TextArea 字符串:
$list = str_replace('|', ' ', $r['db_field_name']);
回答by Va1iant
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
Alay Geleynse 说的是对的,我和你有同样的问题,问题是由于存在转义字符 (\r,\n)。要“取消转义”我使用的变量$var = stripcslashes($var)
并正确显示
回答by Phill Pafford
回答by zod
i have used \p
for text files. try
我已经用于 \p
文本文件。尝试
回答by jim
$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']);
回答by Devaraj
Use like this for dynamically enter each line you can use
像这样使用动态输入您可以使用的每一行
echo chr(13)