27

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. Commented Feb 28, 2013 at 9:17
  • Consider Carriage Return CR and New Line NL are different from HTML Line breaks. Commented 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. Commented 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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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).
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.
19

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;

3 Comments

Absolute legend
Been a while since I was working front end code, and for the life of me (old brain) couldnt' remember this. Thank you.
PHP_EOL is a platform-independent way to add a line break in strings. It ensures the correct line ending is used depending on the system: On Unix-based systems (Linux, macOS), it returns "\n" (LF). On Windows, it returns "\r\n" (CRLF). This approach makes the code portable across different environments. echo "Hello World" . PHP_EOL;
15

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

Stackoverflow reference

Comments

4

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

1 Comment

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...
3
$postfields["message"] = "This is a sample ticket opened by the API\rwith a carriage return";

Comments

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 '

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.