在 PHP 字符串中创建回车符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15131619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:37:59  来源:igfitidea点击:

Create Carriage Return in PHP String?

phpstringcarriage-returnwhmcs

提问by Brett Powell

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.

我们为我们的计费系统编写了一个小的 PHP Hook,在下订单时向我们打开一个新的支持票。除了“Open Ticket”API 函数需要一个字符串作为消息之外,它是有效的,但我们无法弄清楚如何在其中放置回车符。

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

有没有人对如何做到这一点有任何想法? http://docs.whmcs.com/API:Open_Ticket

回答by Bram Gerritsen

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

回车是"\r"。注意双引号!

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.

我认为您希望"\r\n"btw 在您的文本中放置一个换行符,以便它可以在不同的操作系统中正确呈现。

  • Mac: \r
  • Linux/Unix: \n
  • Windows: \r\n
  • 苹果机:\r
  • Linux/Unix:\n
  • 视窗:\r\n

回答by Fran?ois Breton

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

还有跨平台的 PHP 5.0.2 PHP_EOL 常量!

Stackoverflow reference

堆栈溢出参考

回答by berty

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

回答by Themer

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

PHP_EOL 返回与平台上的换行符对应的字符串(LF, \n ou #10 sur Unix, CRLF, \n\r ou #13#10 sur Windows)。

echo "Hello World".PHP_EOL;

回答by Leon Rom

Fragment PHP (in console Cloud9):

片段 PHP(在控制台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 '

1 和 2 之间的区别: "'

回答by Evan TOder

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

我发现添加<br>是想要的。