Jump to content

strange newline behavior


mbeals

Recommended Posts

I have an email formatting script that reads in data from environmental variables, generates an email body and then sends it.

 

I format the body of the email in heredoc with my editor set to unix style newlines.  I have a variable that contains text (including new line chars) that I need to insert into the heredoc.  When I do so, I notice that my '\n' chars are being treated as literal text when viewed in outlook.  So I used str_replace to change them.  I'll just show the results of my trials as it will be easier then explaining it.

 

first, base code:

 

<?php

$subject = "Alert: $SERVICEDESC on $HOSTNAME is $SERVICESTATE";
$priority = "X-Priority: 1\r\nPriority: Urgent\r\nimportance: high"; 
$body  =  <<<EOT
The $SERVICEDESC service on $HOSTNAME has entered the $SERVICESTATE state on $CURRENTTIME.

The plugin performing the check returned the following output:

$SERVICEOUTPUT

$LONGSERVICEOUTPUT
***************************************
Host:              $HOSTNAME
Service:           $SERVICEDESC
Current State:     $SERVICESTATE
Previous State:    $LASTSERVICESTATE 
Valid Time:        $CURRENTTIME 
EOT;


?>

 

In this config, the $LONGSERVICEOUTPUT portion of the email is formatted as:

 

OK - 8 services checked, 8 ok
[ 1] Registered_Modems Registered_Modems OK - 0 \n[ 2] Active_Modems Active_Modems OK - 0 \n[ 3] bitrate bitrate OK - 0 bps \n[ 4] snr SNR: 24 dB   OK \n[ 5] fec_Unerroreds Unerroreds OK - 504421 c \n[ 6] fec_Correcteds Correcteds OK \n[ 7] fec_Uncorrectables Uncorrectables OK \n[ 8] Microreflections Microreflections OK - 0 c

 

The new line chars are printed as text.  Encasing the variable with {} does not help.

 

 

$LONGSERVICEOUTPUT = mysql_escape_string($LONGSERVICEOUTPUT);

results in:

 

OK - 8 services checked, 8 ok
[ 1] Registered_Modems Registered_Modems OK - 0 \\n[ 2] Active_Modems Active_Modems OK - 0 \\n[ 3] bitrate bitrate OK - 0 bps \\n[ 4] snr SNR: 24 dB   OK \\n[ 5] fec_Unerroreds Unerroreds OK - 504421 c \\n[ 6] fec_Correcteds Correcteds OK \\n[ 7] fec_Uncorrectables Uncorrectables OK \\n[ 8] Microreflections Microreflections OK - 0 c

So it appears as if php is parsing the \n as a special char.

 

if I do:

 

$LONGSERVICEOUTPUT = str_replace('\n',"\r\n",$LONGSERVICEOUTPUT);
$LONGSERVICEOUTPUT = mysql_escape_string($LONGSERVICEOUTPUT);

 

I get

 

[ 1] Registered_Modems Registered_Modems OK - 0 \r\n[ 2] Active_Modems Active_Modems OK - 0 \r\n[ 3] bitrate bitrate OK - 0 bps \r\n[ 4] snr SNR: 23 dB  WARNING \r\n[ 5] fec_Unerroreds Unerroreds OK - 504485 c \r\n[ 6] fec_Correcteds Correcteds OK \r\n[ 7] fec_Uncorrectables Uncorrectables OK \r\n[ 8] Microreflections Microreflections OK - 0 c

 

however,

 

$LONGSERVICEOUTPUT = str_replace("\n","\r\n",$LONGSERVICEOUTPUT);
$LONGSERVICEOUTPUT = mysql_escape_string($LONGSERVICEOUTPUT);

 

returns

[ 1] Registered_Modems Registered_Modems OK - 0 \n[ 2] Active_Modems Active_Modems OK - 0 \n[ 3] bitrate bitrate OK - 0 bps \n[ 4] snr SNR: 23 dB  WARNING \n[ 5] fec_Unerroreds Unerroreds OK - 504501 c \n[ 6] fec_Correcteds Correcteds OK \n[ 7] fec_Uncorrectables Uncorrectables OK \n[ 8] Microreflections Microreflections OK - 0 c

 

indicating that it can match the string literal \n but not the new line character.

 

 

if I compromise and do

$LONGSERVICEOUTPUT = str_replace('\n',"\r\n",$LONGSERVICEOUTPUT);

 

I get extra printed newlines

 

[ 1] Registered_Modems Registered_Modems OK - 0

[ 2] Active_Modems Active_Modems OK - 0

[ 3] bitrate bitrate OK - 0 bps

[ 4] snr SNR: 23 dB  WARNING

[ 5] fec_Unerroreds Unerroreds OK - 504501 c

[ 6] fec_Correcteds Correcteds OK

[ 7] fec_Uncorrectables Uncorrectables OK

[ 8] Microreflections Microreflections OK - 0 c

 

 

Is this an issue with heredoc, with windows line feeds or what?  All I want is a single spaced list.

 

 

Link to comment
https://forums.phpfreaks.com/topic/127442-strange-newline-behavior/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.