24

We have written a small PHP Hook for our billing system that opens a new support ticket with us when an order is placed. It works except that for the "Open Ticket" API function, it takes a string for the message, but we cannot figure out how to put carriage returns in it.

I have tried

<p>, <br>, \n, \r\n, etc.

As it appears to just be completely plain text though, all of these are just being read verbatim rather than made into carriage returns.

Does anyone have any thoughts on how this could be done? http://docs.whmcs.com/API:Open_Ticket

Brett Powell
  • 391
  • 2
  • 5
  • 11
  • 2
    Carriage return (`CR`) is `"\r"` only. I think you mean line brakes. – Alvin Wong Feb 28 '13 at 09:17
  • Consider Carriage Return `CR` and New Line `NL` are different from `HTML Line breaks`. – Mahdi Feb 28 '13 at 09:22
  • I'm not familiar with whmcs, but what type of quotation marks are you using? Single or double? In standard PHP, a single quoted string like `'\n'` will render `\n` verbatim. A double quoted string `"\n"` will parse `\n` as a newline character. – Decent Dabbler Feb 28 '13 at 09:22

6 Answers6

51

Carriage return is "\r". Mind the double quotes!

I think you want "\r\n" btw to put a line break in your text so it will be rendered correctly in different operating systems.

  • Mac: \r
  • Linux/Unix: \n
  • Windows: \r\n
Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
  • I had a problem creating html emails with php. Turned out to be an issue in quote usage (email headers need to be defined using " instead of ') This answer fixed it :D thanks. – Tom Groentjes Jan 22 '15 at 16:04
  • Note that only Mac OS 9 (unsupported since 2002) and earlier use \r. Mac OS X is a variant of UNIX and likewise uses \n (0x0A) as a system new-line character (as with other systems, there are still special cases when it is used, such as resetting the carat position to the beginning of a line when outputting to a terminal). – Synexis Apr 26 '16 at 05:54
  • 1
    For anyone else reading this years later, the above only works *exactly* as shown i.e. in double-quotes. `'\r'`, for example, won't work. `"\r"` will work. –  Jan 20 '21 at 07:18
14

There is also the PHP 5.0.2 PHP_EOL constant that is cross-platform !

Stackoverflow reference

Community
  • 1
  • 1
François Breton
  • 1,158
  • 14
  • 24
14

PHP_EOL returns a string corresponding to the line break on the platform(LF, \n ou #10 sur Unix, CRLF, \n\r ou #13#10 sur Windows).

echo "Hello World".PHP_EOL;
Themer
  • 584
  • 4
  • 9
3
$postfields["message"] = "This is a sample ticket opened by the API\rwith a carriage return";
berty
  • 2,178
  • 11
  • 19
3

I find the adding <br> does what is wanted.

Evan TOder
  • 75
  • 1
  • Yep. I continue to be astounded at how difficult it is in PHP to format and output text, especially during development. I realize I don't use PHP often, but... – tim.rohrer May 19 '19 at 19:49
2

Fragment PHP (in console Cloud9):

echo "\n";
echo "1: first_srt=1\nsecnd_srt=2\n";
echo "\n";
echo '2: first_srt=1\nsecnd_srt=2\n';
echo "\n";
echo "==============\n";
echo "\n";

resulting output:

  1: first_srt=1
  secnd_srt=2

  2: first_srt=1\nsecnd_srt=2\n
  ==============

Difference between 1 and 2: " versus '

Leon Rom
  • 537
  • 4
  • 6