php 文本区域中的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6480655/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:30:59  来源:igfitidea点击:

line breaks in a textarea

phphtmltextarealine-breaksnl2br

提问by fl3x7

I know when saving a textarea you can use the nl2br() or str_replace to change the /n to br tags etc. However what im not sure about how to insert line breaks into a textarea. I cant seem to find much about putting the data back into a textarea with those line breaks.

我知道在保存 textarea 时,您可以使用 nl2br() 或 str_replace 将 /n 更改为 br 标签等。但是我不确定如何将换行符插入到 textarea 中。我似乎找不到很多关于将数据放回带有这些换行符的文本区域的信息。

For example I have a form where users can update fields. So the user may enter:

例如,我有一个用户可以更新字段的表单。所以用户可以输入:

foo
bar
baz

When that is saved to the database it would be saved as:

当它被保存到数据库时,它将被保存为:

foo<br />bar<br />baz<br />

Now when that user goes back to that form after a page refresh all the fields are automatically populated with their previous data by taking the data from the database.

现在,当该用户在页面刷新后返回该表单时,通过从数据库中获取数据,所有字段都会自动填充其先前的数据。

However the textarea shows the br tags as text instead of adding in the line breaks. i also tried changing the br tags to /n hoping the textarea would interpret these as line breaks but no joy. As well as this I also tried escaping etc.

但是 textarea 将 br 标签显示为文本,而不是在换行符中添加。我还尝试将 br 标签更改为 /n 希望 textarea 将这些解释为换行符但没有任何乐趣。除此之外,我还尝试逃避等。

So my questions are can this be done? Or more importantly can it be done using HTML/PHP (im using smarty). If that isnt possible can it be done using javascript?

所以我的问题是这可以做到吗?或者更重要的是可以使用 HTML/PHP 来完成(我使用的是 smarty)。如果这是不可能的,可以使用 javascript 来完成吗?

Examples would be appreciated.

示例将不胜感激。

thanks for reading

谢谢阅读

回答by Halcyon

Don't do nl2brwhen you save it to the database. Do nl2brwhen you're displaying the text in HTML. I can strongly recommend to not store any HTML formatting in the database (unless you're using a rich HTML editor as well, in which case it would be silly not to).

nl2br将它保存到数据库时不要这样做。难道nl2br当你在显示HTML文本。我强烈建议不要在数据库中存储任何 HTML 格式(除非您也使用富 HTML 编辑器,在这种情况下不这样做会很愚蠢)。

A newline \nwill just become a newline in the textarea.

换行符\n将成为 textarea 中的换行符。

回答by Francois Deschenes

You could use str_replaceto replace the <br />tags into end of line characters.

您可以使用str_replace<br />标签替换为行尾字符。

str_replace('<br />', PHP_EOL, $textarea);

Alternatively, you could save the data in the database without calling nl2brfirst. That way the line breaks would remain. When you display as HTML, call nl2br. An additional benefit of this approach is that it would require less storage space in your database as a line break is 1 character as opposed to "<br />" which is 6.

或者,您可以在不nl2br先调用的情况下将数据保存在数据库中。这样,换行符将保留。当您显示为 HTML 时,调用nl2br. 这种方法的另一个好处是它需要更少的数据库存储空间,因为换行符是 1 个字符,而不是<br />6 个字符。

回答by Nizam Kazi

Ahh, it is really simple

啊,真的很简单

just add

只需添加

white-space:pre-wrap;

to your displaying element css

到您的显示元素 css

I mean if you are showing result using <p>then your css should be

我的意思是,如果您使用显示结果,<p>那么您的 css 应该是

p{
   white-space:pre-wrap;
}

回答by rahul

You can use following code:

您可以使用以下代码:

$course_description = nl2br($_POST["course_description"]);
$course_description = trim($course_description);

回答by Bashir Noori

Some wrong answers are posted here. instead of replacing \nto <br />, they are replacing <br />to \n

一些错误的答案张贴在这里。他们不是替换\n<br />,而是替换<br />\n

So here is a good answer to store <br />in your mysql when you entered in textarea:

因此,<br />当您在 textarea 中输入时,这是存储在 mysql 中的一个很好的答案:

str_replace("\n", '<br />',  $textarea);

回答by V? Lê Ng?c Anh

The simple way:

简单的方法:

  1. Use this to insert into mysql:

    $msg = $_GET['msgtextarea']; //or POST and my msg field format: text
    $msg = htmlspecialchars($msg, ENT_QUOTES);

  2. And use this for output:

    echo nl2br($br['msg']);

  1. 使用它插入mysql:

    $msg = $_GET['msgtextarea']; //or POST and my msg field format: text
    $msg = htmlspecialchars($msg, ENT_QUOTES);

  2. 并将其用于输出:

    echo nl2br($br['msg']);

回答by Ryan Casas

My recommendation is to save the data to database with Line breaks instead parsing it with nl2br. You should use nl2br in output not input.

我的建议是使用换行符将数据保存到数据库中,而不是使用 nl2br 解析它。您应该在输出而不是输入中使用 nl2br。

For your question, you can use php or javascript:

对于您的问题,您可以使用 php 或 javascript:

PHP:

PHP:

str_replace('<br />', "\n", $textarea);

jQuery:

jQuery:

$('#myTextArea').val($('#myTextArea').val().replace(@<br />@, "\N"));

回答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('|', '&#13;&#10;', $r['db_field_name']);

回答by Joel Enanod Jr

This works on me.

这对我有用。

 str_replace(array("\r", "\n"), '&#10;', $textareavalue);

回答by Gene Bo

From PHP using single quotes for the line break worked for me to support the line breaks when I pass that var to an HTML text area valueattribute

当我将该 var 传递给 HTML 文本区域属性时,使用单引号作为换行符的 PHP 对我来说支持换行符

PHP

PHP

foreach ($videoUrls as $key => $value) {
  $textAreaValue .= $value->video_url . '\n';
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HTML/JS

HTML/JS

$( document ).ready(function() {
    var text = "<?= htmlspecialchars($textAreaValue); ?>";
    document.getElementById("video_urls_textarea").value = text;
});