PHP - 将换行符转换为空格的表单邮件

时间:2012-01-03 19:40:28

标签: php html forms line-breaks

我在用户评论的表单中有一个<textarea>,当内容传递给表单邮件时,换行符正在转换为空格。如何保留表单用户输入的换行符?

相关的php:

$comments = $_REQUEST['comments'];
// This grabs the comments from the submitted form

//...

$to = $configEmail;

$subject = "Website Order Received: $offer";

$contents = "blah blah blah...";
if (!empty ($comments)) {
    $contents = $contents."\nComments: $comments\n\n";
}

//...

mail($to, $subject, $contents);

在表单的HTML末尾...(如果提交错误,则将注释放入表单中,因此数据不会丢失)

<label>Comments / Questions</label>
<textarea name="comments"><?php echo $comments; ?></textarea>

如果我输入:

line 1
line 2
line 3

如果提交的表单有错误,那么$comments = $_REQUEST['comments'];肯定会保留换行符。但纯文本电子邮件给了我:

line 1 line 2 line 3

如何保留换行符?

1 个答案:

答案 0 :(得分:2)

问题是来自textarea的换行符是\n而不是<br> .. 因此,请在发送邮件之前将\n替换为<br> ..

$body= str_replace("[enter]", "\n",$body);

Rember用户在“\ n”...

中双击