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

问题描述:

我将以下内容保存到我的$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"; 

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

+0

'\ r'是不是一个新的生产线; '\ n'是新的一行字符。 'www.domain.com/validateemail ...'不是网址。 URL以协议('http://','https://'等)开头。没有它,电子邮件客户端不会将其检测为URL,也不会从其创建链接。 – axiac

+0

@axiac'\ r','\ n'和'\ r \ n'是EOL符号,它们来自不同的操作系统( - ; – Neodan

+0

@Neodan [使用'\ r'作为EOL的操作系统]( https://en.wikipedia.org/wiki/Newline#Representations)要么是晦涩的(小众产品),要么已经停用多年,其中最着名的是[“Classic”Mac OS](https://en.wikipedia .org/wiki/Classic_Mac_OS)替换为[Mac OS X](https://en.wikipedia.org/wiki/Mac_OS_X_10.0)(又名“OS X”,又名[macOS](https:// en .wikipedia.org/wiki/MacOS))15年前。macOS和各种Linux发行版使用'\ n'作为EOL,Windows使用'\ r \ n'作为EOL,但它们都没有将单个'\ r'识别为EOL。 – axiac

问题在你的代码:

  • \r是不是一个新的生产线; \n是新的一行字符。
  • www.domain.com/validateemail?...不是网址。 URL以协议(http://https://等)开头。没有它,电子邮件客户端不会将其检测为URL,也不会从其创建链接。

有几种方法可以编写易于阅读和修改的代码。例如,您可以使用heredoc syntax作为字符串。它允许您在多行上编写文本,而无需担心如何编写换行符。另外,PHP parses it for variables和转义序列的方式与double quoted strings相同。

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

如果要发送HTML邮件,你可以使用相同的技术来生成电子邮件身体,但不要忘了,HTML不关心在源代码中的换行。将文本包装成<p> HTML元素以生成段落并使用HTML元素在段落内强制新行。

的代码可能是这样的:

$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; 
+0

是\ r是古老的EOL,但仍然...它是EOL。查看http://php.net/manual/en/function.nl2br.php的'Description'部分 – Neodan

不同的OS使用行结束的不同的符号(EOL):

  • \r\n - CRLF(视窗)
  • \n - LF(Unix和OS X)
  • \r - CR(苹果)

如果以HTML格式打印此消息,则必须将EOL符号更改为<br>标记,因为EOL符号被忽略。

您可以使用nl2br进行转换。

+0

我想知道为什么我的回答是downvoted。 – Neodan