电子邮件中的 PHP 换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3206531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:00:14  来源:igfitidea点击:

PHP new line break in emails

phpnewline

提问by Michael

I have the following code:

我有以下代码:

$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);

The problem is that the new line break(\n)is not working.

问题是新换行符(\n)不起作用。

回答by Brendan Bullen

Try \r\nin place of \n

尝试\r\n代替\n

The difference between \n and \r\n

\n 和 \r\n 的区别

It should be noted that this is applicable to line returns in emails. For other scenarios, please refer to rokjarc's answer.

应该注意的是,这适用于电子邮件中的行返回。其他场景请参考rokjarc的回答

回答by Rok Jarc

I know this is an old question but anyway it might help someone.

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

I tend to use PHP_EOLfor this purposes (due to cross-platform compatibility).

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

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

If you're planning to show the result in a browser then you have to use "<br>".

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

EDIT:since your exact question is about emails, things are a bit different. For pure text emails see Brendan Bullen's accepted answer. For HTML emails you simply use HTML formatting.

编辑:由于您的确切问题是关于电子邮件,因此情况有所不同。对于纯文本电子邮件,请参阅 Brendan Bullen接受的答案。对于 HTML 电子邮件,您只需使用 HTML 格式。

回答by Harold1983-

Are you building this string using single or double quotes? \r and \n only work with double quotes, as well as embedded variables. For example:

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

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

will output:

将输出:

Hello \n $foo!

But:

但:

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

will output:

将输出:

Hello
bar!

回答by jeroen

If you output to html or an html e-mail you will need to use <br>or <br />instead of \n.

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

If it's just a text e-mail: Are you perhaps using 'instead of "? Although then your values would not be inserted either...

如果它只是一个文本电子邮件:您是否可能使用'而不是"?虽然那样你的值也不会被插入......

回答by Amjad Abu Saa

you can use "Line1<br>Line2"

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

回答by Martin Vseticka

EDIT:Maybe your class for sending emails has an option for HTML emails and then you can use <br />

编辑:也许您发送电子邮件的课程有一个 HTML 电子邮件选项,然后您可以使用 <br />

1) Double-quotes

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";

If you use double-quotes then \n will work (there will be no newline in browser but see the source code in your browser - the \n characters will be replaced for newlines)

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

2) Single quotesdoesn't have the effect as the double-quotes above:

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';

all characters will be printed as is (even variables!)

所有字符都将按原样打印(甚至变量!)

3) Line breaks in HTML

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 />";
  • There will be line breaks in your browser and variables will be replaced with their content.
  • 浏览器中会有换行符,变量将被替换为它们的内容。

回答by m0g

if you are outputting the code as html - change /n -->
and do echo $message;

如果您将代码输出为 html - 更改 /n -->
并执行 echo $message;

回答by m0g

When we insert any line break with a programming language the char code for this is "\n". php does output that but html can't display that due to htmls line break is
. so easy way to do this job is replacing all the "\n" with "
". so the code should be

当我们用编程语言插入任何换行符时,其字符代码是“\n”。php 确实输出,但由于 htmls 换行符,html 无法显示
。完成这项工作的简单方法是将所有“\n”替换为“
”。所以代码应该是

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

after adding this code you wont have to use pre tag for all the output oparation.

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

copyed this ans from this website:

从这个网站复制这个答案