PHP string concatenation and newlines

I need a little help understanding PHP behavior that doesn't make sense to me, in advance for the help.

First of all, is there a difference between using \r\n and \n\r ? I think logically not, but I think there should be a practical reason why they are listed separately in the PHP document. I am currently using \r\n , but sometimes I get a line break, sometimes I don’t do this, they confuse it.

Secondly, I know that if I repeated the information in the browser, I would use the nl2br() function, but I do not display the information in the browser, it is collected and combined into a string, which is then sent via mail() and what’s there, where the visual inconsistency is displayed.

Here is a script, pretty straight forward, nothing scary here:

 <?php $browser = $_SERVER['HTTP_USER_AGENT']; $page = $_SERVER['PHP_SELF']; $ip = $_SERVER['REMOTE_ADDR']; $host = gethostbyaddr($_SERVER['REMOTE_ADDR']); $from_page = $_SERVER['HTTP_REFERER']; $time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']); $uri = $_SERVER['SCRIPT_URI']; $to = "***"; $subject = "Visitor Log"; $message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . "Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri; $headers = "From: ***" . "\r\n" . "Reply-To: ***" . "\r\n" . "X-Mailer: PHP/" . phpversion(); mail($to,$subject,$message,$headers); ?> 

But, as you can see, I have lines that are added to $message for the explicit readability of the letter I receive. But sometimes letters arrive, and everything goes on a separate line, as it should be, and sometimes not. So I thought that the method of adding a line break is not the best and that I am doing something wrong.

So the question is what should I use in order to get the right line breaks if what I use is wrong, and if it is right, why doesn't it always work? Your help in adding some clarity to this will be greatly appreciated.

If it matters or matters, I am using a Linux 5 server (5.5.28) with Apache.


UPDATED RESPONSE

Thank you for your help, but I ended up fixing it as follows and even added a content type for a good grade (TY @TOM).

 <?php $browser = $_SERVER['HTTP_USER_AGENT']; $page = $_SERVER['PHP_SELF']; $ip = $_SERVER['REMOTE_ADDR']; $host = gethostbyaddr($_SERVER['REMOTE_ADDR']); $from_page = $_SERVER['HTTP_REFERER']; $time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']); $uri = $_SERVER['SCRIPT_URI']; $to = "***"; $subject = "Visitor Log"; $message = "IP Address : " . $ip . " ( HOST: " . $host . " )" . "\r\n"; $message .= "Browser : " . $browser . "\r\n"; $message .= "From : " . $from_page . "\r\n"; $message .= "Page : " . $page . "\r\n"; $message .= "Time : " . $time . "\r\n"; $message .= "Script : " . $uri; $headers = "From: ***" . "\r\n"; $headers .= "Reply-To: ***" . "\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; $headers .= "Content-Type: text/html; charset=\"utf-8\"" . "\r\n"; mail($to,$subject,nl2br($message),nl2br($headers)); ?> 

NOTE The new formatting is not part of the solution, it is only for me. The solution abandons the simple one and comes with html and uses the nl2br function to force breaks, not elegant ones, and not what I wanted entirely, but it works.

+5
source share
2 answers

My best guess is that sometimes you send the text / plain and sometimes the text / html .. (since you are not specifying anything, maybe mail () or the mail server or client takes the best guess?)

I would try to get it to send text / html and use

 <br/> 

with nl2br or force it as text / plain and use \ n

as...

 $message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . 'Content-Type: text/html; charset="utf-8"'."\r\n"."Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri; 

.. if this is the way you force it to send mail as html, I'm not sure about the header

+1
source

The fact is that you do not pass the content-type in the email header, so the email client may interpret it as plain-text or text/html depending on the provider. It is best to pass the content-type parameter to the email header.

  • content-type:text/plain : \n as a line break
  • content-type:text/html : </br> as a line break
+1
source

All Articles