2017-09-05 26 views
-1

我將以下內容保存到我的$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

回答

1

問題在你的代碼:

  • \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

1

不同的OS使用行結束的不同的符號(EOL):

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

如果以HTML格式打印此消息,則必須將EOL符號更改爲<br>標記,因爲EOL符號被忽略。

您可以使用nl2br進行轉換。

+0

我想知道爲什麼我的回答是downvoted。 – Neodan

相關問題