PHP长字符串不带换行符
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很容易:
That's easy:
您可以从字符串中删除换行符:
您仍然应该使用连接运算符 (.) 来处理此类事情。 如果您使用操作码缓存 (APC),则这里的性能损失可以忽略不计,并且在将数据库访问和附加逻辑(包括循环)纳入运行时方程时,这实际上不会引起任何注意。
更新:
请阅读下面的页面。 上述结论在文本中并不容易得到,但仔细检查应该表明它是有效的。
http://blog.golemon.com/ 2006/06/how-long-is-piece-of-string.html
You could strip newline characters from the string:
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
这是正确的代码,解决了所有问题:
它基于 Lior Cohen 的答案,但它也删除了回车符“\r”。
This is the right code, that solved all issues:
It is based on Lior Cohen's answer, but it also strips carriage returns "\r".
为什么不:
你只是追加多行
why not:
you are just appending over multi lines
使用 heredoc 语法(或nowdoc,根据您的需要,检查文档链接):
但这很糟糕......抱歉:-)
With the heredoc syntax (or nowdoc, depending on your needs, check the documentation link):
But this sucks... sorry :-)