php 写入 MySQL 时保留 TextArea 的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5048849/
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
Preserve Line Breaks From TextArea When Writing To MySQL
提问by Hirvesh
I'm using a textarea to enable users to input comments. However, if the users enters new lines, the new lines don't appear when they are outputted. Is there any way to make the line breaks stay.
我正在使用 textarea 使用户能够输入评论。但是,如果用户输入新行,则输出时不会出现新行。有什么办法可以让换行符保持不变。
Any idea how do preserve the line breaks?
知道如何保留换行符吗?
回答by superUntitled
Two solutions for this:
对此有两种解决方案:
PHP function
nl2br()
:e.g.,
echo nl2br("This\r\nis\n\ra\nstring\r"); // will output This<br /> is<br /> a<br /> string<br />
Wrap the input in
<pre></pre>
tags.
PHP函数
nl2br()
:例如,
echo nl2br("This\r\nis\n\ra\nstring\r"); // will output This<br /> is<br /> a<br /> string<br />
将输入包装在
<pre></pre>
标签中。
回答by hiroki
Here is what I use
这是我使用的
$textToStore = nl2br(htmlentities($inputText, ENT_QUOTES, 'UTF-8'));
$inputText
is the text provided by either the form or textarea.
$textToStore
is the returned text from nl2br
and htmlentities
, to be stored in your database.
ENT_QUOTES
will convert both double and single quotes, so you'll have no trouble with those.
$inputText
是由表单或文本区域提供的文本。
$textToStore
是从nl2br
和返回的文本htmlentities
,将存储在您的数据库中。
ENT_QUOTES
将转换双引号和单引号,因此您不会遇到这些问题。
回答by Hirvesh
Got my own answer: Using this function from the data from the textarea solves the problem:
得到了我自己的答案:从 textarea 的数据中使用这个函数可以解决问题:
function mynl2br($text) {
return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));
}
More here: http://php.net/nl2br
更多信息:http: //php.net/nl2br
回答by Jayendra Bariya
i am using this two method steps for preserve same text which is in textareato store in mysql and at a getting time i can also simply displaying plain text.....
我正在使用这两个方法步骤来保留textarea 中的相同文本以存储在 mysql 中,并且有时我也可以简单地显示纯文本.....
step 1:
第1步:
$status=$_POST['status'];<br/>
$textToStore = nl2br(htmlentities($status, ENT_QUOTES, 'UTF-8'));
In query enter $textToStore
....
在查询中输入$textToStore
....
step 2:
第2步:
write code for select query...and direct echo values....
为选择查询编写代码......和直接回显值......
It works
有用
回答by UbiQue
This works:
这有效:
function getBreakText($t) {
return strtr($t, array('\r\n' => '<br>', '\r' => '<br>', '\n' => '<br>'));
}
回答by Libin Thomas
function breakit($t) {
return nl2br(htmlentities($t, ENT_QUOTES, 'UTF-8'));
}
this may help you
这可能会帮助你
pass the textarea wal
通过 textarea wal
回答by Duncan
why make is sooooo hard people when it can be soooo easy :)
为什么 make 是那么难的人,而它可以如此简单:)
//here is the pull from the form
$your_form_text = $_POST['your_form_text'];
//line 1 fixes the line breaks - line 2 the slashes
$your_form_text = nl2br($your_form_text);
$your_form_text = stripslashes($your_form_text);
//email away
$message = "Comments: $your_form_text";
mail("[email protected]", "Website Form Submission", $message, $headers);
you will obviously need headers and likely have more fields, but this is your textarea take care of
您显然需要标题并且可能有更多字段,但这是您的 textarea 照顾