Linux 在 textarea 上看不到新行 - 可能是什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677026/
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
Can't see new lines on textarea - what could the problem be?
提问by MEM
I have a php string with a lot of information to be displayed inside a textarea html element.
我有一个 php 字符串,其中包含要在 textarea html 元素中显示的大量信息。
I don't have access to that textarea nor to the script (if any) that generates it.
我无权访问该文本区域,也无权访问生成它的脚本(如果有)。
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
$somestring 未与trim 或filter_var 一起“工作”。没有。
On the textfield, I get the \n printed on the textarea hence, not interpreted.
在文本字段上,我将 \n 打印在文本区域上,因此不会被解释。
What can I try in order to have those new lines applied?
我可以尝试什么来应用这些新行?
Thanks in advance.
提前致谢。
采纳答案by Frxstrem
\n
, \r
and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n
and \r
.
\n
,\r
和其他反斜杠转义字符仅适用于双引号和 heredoc。在单引号和 nowdoc(heredoc 的单引号版本)中,它们被视为文字\n
和\r
.
Example:
例子:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
在PHP 手册中阅读有关此行为的更多信息
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
编辑:
单引号和双引号行为不同的原因是因为它们在不同的情况下都需要。
For instance, if you would have a string with a lot of new lines, you would use double quotes:
例如,如果你有一个包含很多新行的字符串,你可以使用双引号:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
但是,如果您将使用带有大量反斜杠的字符串,例如文件名(在 Windows 上)或正则表达式,您将使用单引号来简化它,并通过忘记转义反斜杠来避免出现意外问题:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
回答by kieran
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
尝试用 " (双引号) 而不是 ' (单引号) 包裹 $somestring
回答by Your Common Sense
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string<-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.
http://php.net/types.string<--阅读
这篇文章非常有用,它是 PHP 知识的基石,没有它就不可能使用 PHP。
与大多数仅供快速参考的手册页不同,这一页是每个开发人员都应该牢记的一页。