【问题标题】:How to convert carriage returns into line breaks in php when I receive the email?当我收到电子邮件时,如何将回车转换为 php 中的换行符?
【发布时间】:2013-08-26 05:50:09
【问题描述】:

我有一个带有表单的网站。表格被提交到一个电子邮件地址,所以我可以从那里阅读它。我一直在通过给自己发送电子邮件来测试它,但是当我收到消息时,我希望它会像这个例子:

姓名:某某

号码:123456789

电子邮件:someguy@someweb.com

消息:

羊角面包甘草杏仁饼。大枣纸杯蛋糕纸杯蛋糕纸杯蛋糕丹麦棉花糖。松饼羊角面包苹果派。

Chupa chups 果冻-o 燕麦蛋糕糖李子羊角面包 tootsie 卷。糖果手杖浇头柠檬滴。软糖 果冻 软糖 布朗尼 halvah 芝麻脆棒棒糖应用程序。

棉花糖焦糖糖梅果冻杏仁芝麻脆片丹麦粉芝麻脆片。 Bonbon甘草松饼甘草棉花糖甜点燕麦蛋糕。棉花糖焦糖甜点蛋糕派果冻豆提拉米苏。蛋糕应用提拉米苏松饼马卡龙派甜甜圈。

我是这样收到的:

羊角面包甘草杏仁饼。大枣纸杯蛋糕纸杯蛋糕纸杯蛋糕丹麦棉花糖。松饼羊角面包苹果派。 Chupa chups jelly-o 燕麦蛋糕糖李子羊角面包 tootsie 卷。糖果手杖浇头柠檬滴。 Gummies jelly gummies brownie halvah sesame snap candy canes applicake。棉花糖焦糖糖梅果冻马卡龙芝麻脆丹麦粉芝麻脆。 Bonbon甘草松饼甘草棉花糖甜点燕麦蛋糕。棉花糖焦糖甜点蛋糕派果冻豆提拉米苏。蛋糕应用提拉米苏松饼马卡龙派甜甜圈。

所有换行符都消失了,消息是一条直线。

我对 PHP 还不是很熟悉,所以如果您能以最简单的方式帮助我,将不胜感激。

这是我的 php 注释部分代码:

<?php
//If the form is submitted
if(isset($_POST['submit'])) {
$emailText = $_POST['message'];
str_replace("\r", "\n", $emailText);

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
    $email = trim($_POST['email']);
}

//Check to make sure sure that a phone number is submitted
if(trim($_POST['phone']) == '')  {
    $hasError = true;
} else if (!preg_match("/^[1-9][0-9]{0,10}$/", trim($_POST['phone']))) {
        $hasError = true;
    } else {
    $phone = trim($_POST['phone']);
}

//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
    $hasError = true;
} else {
    $subject = trim($_POST['subject']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'myemail@gmail.com'; //Put your own email address here
    $body = "<strong>Name:</strong> $name <br><br><strong>Email:</strong> $email <br><br><strong>Contact:</strong> $phone <br><br><strong>Subject:</strong> $subject <br><br><strong>Message:</strong><br><br> $comments";
    $headers = 'From: Ucorp Online Form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
}
 ?>

【问题讨论】:

  • 我不确定nl2br($text)是否对你有帮助
  • nl2br() 会成功的。
  • 您的问题非常不准确。所以无法真正回答。我建议您使用提供的edit 按钮为您的问题添加更多详细信息......“我收到他们喜欢的东西”是什么意思?您是否将它们输出到浏览器?您是将它们写入文件还是通过电子邮件发送?你如何想象它们?而且,在换行方面非常重要:您的 http 服务器使用什么操作系统?
  • 我不确定将 nl2br() 放在哪里,因为我从免费下载的预制表单中获得了 php 代码,但它没有该功能
  • @arkascha:我想在我的电子邮件中查看它们。评论/消息区域没有换行符,因为它最初放在 textarea 上。

标签: php textarea


【解决方案1】:

您正在寻找nl2br()。将值设置为$comments后应用:

if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
    $comments = nl2br($comments);
}

【讨论】:

    【解决方案2】:

    试试 php 的 `str_replace() 方法。

    $emailText = $_POST['message'];
    str_replace("\r", "\n", $emailText);
    

    http://www.php.net/manual/en/function.preg-replace.php

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      相关资源
      最近更新 更多