PHP长字符串不带换行符

发布于 2024-08-02 00:11:23 字数 175 浏览 4 评论 0原文

我在 php 中有一个很长的字符串,它不包含新行('\n')。

我的编码约定不允许行长超过 100 个字符。

有没有办法将我的长字符串分成多行而不使用 . 效率较低的运算符 - 我不需要连接 2 个字符串,因为它们可以作为 单字符串。

谢谢!

I have a long string in php which does not contain new lines ('\n').

My coding convention does not allow lines longer than 100 characters.

Is there a way to split my long string into multiple lines without using the . operator which is less efficient - I don't need to concat 2 strings as they can be given as a
single string.

Thanks!

Y

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陪我终i 2024-08-09 00:11:23

这很容易:

$str  = 'long text long text long text long text long text';
$str .= 'long text';

That's easy:

$str  = 'long text long text long text long text long text';
$str .= 'long text';
诺曦 2024-08-09 00:11:23

您可以从字符串中删除换行符:

  $myStr = "
    here is my string
    and it is spread across multiple lines
  ";
  $myStr = str_replace("\n", "", $myStr);  

您仍然应该使用连接运算符 (.) 来处理此类事情。 如果您使用操作码缓存 (APC),则这里的性能损失可以忽略不计,并且在将数据库访问和附加逻辑(包括循环)纳入运行时方程时,这实际上不会引起任何注意。

更新:

请阅读下面的页面。 上述结论在文本中并不容易得到,但仔细检查应该表明它是有效的。

http://blog.golemon.com/ 2006/06/how-long-is-piece-of-string.html

You could strip newline characters from the string:

  $myStr = "
    here is my string
    and it is spread across multiple lines
  ";
  $myStr = str_replace("\n", "", $myStr);  

You should still use the concatenation operator (.) for such things. The performance penalty here is negligible if you're using opcode caching (APC) and this would really not be anything noticeable when taking DB access and additional logic (including loops) into the run time equation.

Update:

Give the page below a read. The conclusion above is not readily available in the text, but a careful examination should show it is valid.

http://blog.golemon.com/2006/06/how-long-is-piece-of-string.html

守护在此方 2024-08-09 00:11:23

这是正确的代码,解决了所有问题:

$myStr = "
    here is my string
    and it is spread across multiple lines
  ";
  $myStr = str_replace(array("\r","\n"), "", $myStr);  

它基于 Lior Cohen 的答案,但它也删除了回车符“\r”。

This is the right code, that solved all issues:

$myStr = "
    here is my string
    and it is spread across multiple lines
  ";
  $myStr = str_replace(array("\r","\n"), "", $myStr);  

It is based on Lior Cohen's answer, but it also strips carriage returns "\r".

我要还你自由 2024-08-09 00:11:23

为什么不:

 $myStr = "here is my string "
   . "and it is spread across "
   . "multiple lines";

你只是追加多行

why not:

 $myStr = "here is my string "
   . "and it is spread across "
   . "multiple lines";

you are just appending over multi lines

旧情勿念 2024-08-09 00:11:23

使用 heredoc 语法(或nowdoc,根据您的需要,检查文档链接):

$multiline = <<<EOT
My name is "$name". I am printing some $foo->foo.
This should print a capital 'A': \x41
EOT;
$singleline = str_replace("\n","",$multiline);

但这很糟糕......抱歉:-)

With the heredoc syntax (or nowdoc, depending on your needs, check the documentation link):

$multiline = <<<EOT
My name is "$name". I am printing some $foo->foo.
This should print a capital 'A': \x41
EOT;
$singleline = str_replace("\n","",$multiline);

But this sucks... sorry :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文