PHP新邮件中断行

时间:2010-07-08 17:56:34

标签: php newline

我有以下代码:

$message = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\n
The seller, $bid_user is making this offer.\n
\nItem Title : $title\n

\nAll the best,\n
$bid_user\n
$email\n
";

echo $message;
$htmlContent = $baseClass->email_friend($item_number, $title, $bid_price, $bid_user, $mcat, $message, $email, $VIEWSTATE, $EVENTVALIDATION, $URL);

问题是新换行符 (\n)无效。

8 个答案:

答案 0 :(得分:44)

尝试\r\n代替\n

The difference between \n and \r\n

应该注意,这适用于电子邮件中的换行。对于其他场景,请参阅rokjarc的answer

答案 1 :(得分:30)

我知道这是一个老问题,但无论如何它可能对某人有帮助。

我倾向于将PHP_EOL用于此目的(由于跨平台兼容性)。

echo "line 1".PHP_EOL."line 2".PHP_EOL;

如果您计划在浏览器中显示结果,则必须使用"<br>"

编辑:,因为您的确切问题是关于电子邮件,事情有点不同。 对于纯文本电子邮件,请参阅Brendan Bullen的accepted answer。对于HTML电子邮件 你只需使用HTML格式。

答案 2 :(得分:19)

您是使用单引号还是双引号构建此字符串? \ r和\ n仅使用双引号以及嵌入变量。例如:

$foo = 'bar';
echo 'Hello \n $foo!';

将输出:

Hello \n $foo!

可是:

$foo = 'bar';
echo "Hello \n $foo!";

将输出:

Hello
bar!

答案 3 :(得分:6)

如果您输出到html或html电子邮件,则需要使用<br><br />代替\n

如果只是文本电子邮件:您是否正在使用'而不是"?虽然那么你的值也不会被插入...

答案 4 :(得分:6)

您可以使用"Line1<br>Line2"

答案 5 :(得分:4)

编辑:也许您发送电子邮件的课程有HTML电子邮件选项,然后您可以使用&lt; br /&gt;

1)双引号

$output = "Good news! The item# $item_number  on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n";

如果您使用双引号,则\ n将起作用(浏览器中没有换行符,但在浏览器中查看源代码 - \ n字符将替换为换行符)

2)单引号不具备上述双引号的效果:

$output = 'Good news! The item# $item_number  on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n';

所有字符都将按原样打印(即使是变量!)

3) HTML中的换行符

$html_output = "Good news! The item# $item_number  on which you placed a bid of <br />$ $bid_price is now available for purchase at your bid price.<br />The seller, $bid_user is making this offer.<br /><br />Item Title : $title<br /><br />All the best,<br /> $bid_user<br />$email<br />";
  • 您的浏览器会有换行符,变量将替换为其内容。

答案 6 :(得分:2)

如果您输出的代码为html - change / n - &gt;
并做回声$ message;

答案 7 :(得分:2)

当我们使用编程语言插入任何换行符时,其代码为“\ n”。 php输出但是html无法显示由于htmls换行符是
。如此简单的方法就是用“
”替换所有“\ n”。所以代码应该是

str_replace("\n","<br/>",$str);

添加此代码后,您不必为所有输出操作使用pre标记。

copyed this ans from this website

相关问题