Php 新行转义需要如何格式化才能正确显示消息

Php 新行转义需要如何格式化才能正确显示消息,php,line-breaks,Php,Line Breaks,我已将以下内容保存到我的$msg变量中。在显示器上,我的目标是用新行正确打印格式。我不确定我能做什么不同的事情。顺便说一句,我可能会把这条信息弄错 $msg ="Hey ".$row['emailName']."\rYou recently signed up to receive email updates from us, and we wanted to verify your email address.\nIf you signed up to receive updates ple

我已将以下内容保存到我的
$msg
变量中。在显示器上,我的目标是用新行正确打印格式。我不确定我能做什么不同的事情。顺便说一句,我可能会把这条信息弄错

$msg ="Hey ".$row['emailName']."\rYou recently signed up to receive email updates from us, and we wanted to verify your email address.\nIf you signed  up to receive updates please press the confirmation link below:\nwww.domain.com/validateemail?email=".$row['emailAddress']."&emailID=".$row['emailID']."&emailed=1\nThe request came from".$row['signupIP']."\nIf you did not make this request, you can ignore this email\n \r Thanks,\rKathleen Williams\rHead Trainer";

问题是它没有显示换行符。

不同的操作系统使用不同的符号表示换行结束(EOL):

  • \r\n
    -CRLF(Windows)
  • \n
    -LF(Unix和OS X)
  • \r
    -CR(Mac)
如果以HTML打印此消息,则必须将EOL符号更改为

标记,因为EOL符号将被忽略

您可以使用转换。代码中的问题:


  • \r
    不是新行<代码>\n是新行字符
  • www.domain.com/validateemail?..
    不是URL。URL以协议开头(
    http://
    https://
    等)。没有它,电子邮件客户端不会将其检测为URL,也不会从中创建链接
有几种方法可以编写易于阅读和修改的代码。例如,可以使用For字符串。它允许您在多行上写入文本,而无需担心如何写入换行符。此外,PHP和转义序列与它使用的方式相同


//在`
\r
行之后开始的文本不是新行<代码>\n
是新行字符
www.domain.com/validateemail…
不是URL。URL以协议开头(
http://
https://
等)。没有它,电子邮件客户端就不会将其检测为URL,也不会从中创建链接。@axiac
\r
\n
\r\n
是EOL符号,只是它们来自不同的操作系统(-@Neodan)要么是晦涩难懂的(小众产品),要么多年来一直不存在。其中最著名的是被(又名“OS X”)取代的macOS和无数Linux发行版使用
\n
作为EOL,Windows使用
\r\n
作为EOL,但没有一个版本将单个
\r
识别为EOL。@axiac如果你错了,
\r
(CR)仍然被支持并被识别为EOL。是的,它非常古老且不受欢迎(很少使用)EOL,但它是EOL。只需在文本编辑器、Firefox等上试用即可。您将看到它仍然有效。@Neodan“只需在文本编辑器上试用即可”--我在记事本和
vi
(Windows和Linux附带的默认文本编辑器)中试用过它。他们不认为
\r
是一个终点。Linux附带的大多数工具也是如此(
sed
wc
grep
head
tail
等等)。我手头没有macOS(今天晚些时候我会检查一下)但我怀疑macOS上的相同工具是否能将
\r
识别为行尾字符。我们在2017年,
\r
因为EOL是一个遥远过去的童话故事。是的\r是古老的EOL,但仍然…它是EOL。请查看本节的
描述
部分
// The text starting after the line `<<< END` and ending before 
// the marker provided after `<<<` is a string.
// It is stored into the $msg variable.
$textBody = <<< END_TEXT
Hey {$row['emailName']}
You recently signed up to receive email updates from us, and we wanted to verify your email address.
If you signed  up to receive updates please press the confirmation link below:

    http://www.example.com/validateemail?email={$row['emailAddress']}&emailID={$row['emailID']}&emailed=1

The request came from {$row['signupIP']}.
If you did not make this request, you can ignore this email.

Thanks,
Kathleen Williams
Head Trainer

END_TEXT;
$htmlBody = <<< END_HTML
<p>Hey {$row['emailName']}</p>
<p>You recently signed up to receive email updates from us,
and we wanted to verify your email address.<br>
If you signed  up to receive updates please press this 
<a href="http://www.example.com/validateemail?email={$row['emailAddress']}&amp;emailID={$row['emailID']}&amp;emailed=1">confirmation link</a>.</p>

<p>The request came from {$row['signupIP']}.<br>
If you did not make this request, you can ignore this email.</p>

<p>Thanks,<br>
Kathleen Williams<br>
Head Trainer</p>

END_HTML;