25

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

3
  • 2
    Carriage return (CR) is "\r" only. I think you mean line brakes.
    – Alvin Wong
    Feb 28, 2013 at 9:17
  • Consider Carriage Return CR and New Line NL are different from HTML Line breaks.
    – Mahdi
    Feb 28, 2013 at 9: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. Feb 28, 2013 at 9:22

6 Answers 6

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
3
  • 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. Jan 22, 2015 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, 2016 at 5: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.
    – user743873
    Jan 20, 2021 at 7:18
15

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;
2
  • Absolute legend
    – hallodom
    Nov 24, 2022 at 0:15
  • Been a while since I was working front end code, and for the life of me (old brain) couldnt' remember this. Thank you. Feb 17 at 21:00
14

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

Stackoverflow reference

3
$postfields["message"] = "This is a sample ticket opened by the API\rwith a carriage return";
0
3

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

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, 2019 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 '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.