为什么从表单发送的存储textarea数据中添加了额外的换行符

时间:2012-12-05 14:14:33

标签: php html textarea

我有一个Textarea,其中包含:

Test can't talk<br />
Test

当我将其发送到我的预览页面时,我使用它:

$Comments = htmlspecialchars("$_POST[CustComments]", ENT_QUOTES);
$Comments = str_replace("\n","<br />", $Comments);

然后在预览页面上显示:

Test can't talk<br />
Test

当我回去编辑它时,在编辑页面代码中我有这个。

$Comments = str_replace("<br />","\n", $_POST['CustComments']);
$Comments = htmlspecialchars($Comments, ENT_QUOTES);

但它显示了这一点:

Test can't talk

Test

这个额外的休息来自何处以及如何摆脱它?


更新 根据编辑页面上的建议,我有这个。

$Comments = htmlspecialchars($_POST['CustComments'], ENT_QUOTES);

这表明了这一点:

Test can't talk
<br />Test

这个额外的<br />又来自哪里?如果我使用strreplace,则会显示如下

Test can't talk

Test

使用这个:

$Comments = nl2br($_POST['CustComments']);

它显示了这个:

Test can't talk<br />
<br />Test

再次添加随机<br />

2 个答案:

答案 0 :(得分:1)

在视图页面上需要使用:

$Comments = htmlspecialchars("$_POST[CustComments]", ENT_QUOTES);
$Comments = str_replace("\r\n","<br />",$Comments);

在编辑页面上使用:

$Comments = str_replace("<br />","\n",$_POST['CustComments'])

在提交页面上:

function keepSafe($value) {
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    if (!is_numeric($value)) {
        $value = "'" . mysql_real_escape_string($value) . "'";
    }
    return $value;
}
$Comments = keepSafe($_POST['CustComments']);

像魅力一样。

答案 1 :(得分:0)

为什么使用str_replace()?试试这个:

$Comments = nl2br($Comments);