我在用户评论的表单中有一个<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
如何保留换行符?
答案 0 :(得分:2)
问题是来自textarea的换行符是\n
而不是<br>
..
因此,请在发送邮件之前将\n
替换为<br>
..
$body= str_replace("[enter]", "\n",$body);
Rember用户在“\ n”...
中双击