如何格式化数组中的长字符串
我应该如何在源代码中格式化很长的字符串?
我遵循规则,该行代码不应超过 80 个字符。
(其他规则是 Zend Framework 格式化标准)
例如
protected $_messages = array(
'key1' => 'very, very long string lorem ipsum dolor sit amet…',
'key2' => 'this one it very long too, and exceeds 80 characters len…'
);
How should I format very long strings in my source code?
I follow the rule, that line of code should not be longer than 80 characters.
(The other rules are Zend Framework formatting standard)
e.g.
protected $_messages = array(
'key1' => 'very, very long string lorem ipsum dolor sit amet…',
'key2' => 'this one it very long too, and exceeds 80 characters len…'
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就我个人而言,我有时喜欢长行 - 如果您在编辑器中关闭换行,它可以使内容更具可读性。 “弦”中的断点。 “细绳” 。 “细绳”;在我看来,格式只是混乱。
本质上,规则就是用来打破的。使用对你来说最可读的东西,而不是遵循别人的可读想法。
Personally I like long lines on occasion - if you turn off wrapping in your editor it can make things more readable. The breaks in the "string" . "string" . "string"; format is just messy imo.
Rules are there to be broken, essentially. Use whatever is most readable for you rather than conforming to someone else's idea of readable.
如果您无法将值存储在数据库/文件中(按照 adam/Peter),并且您绝对需要根据 Zend Framework 指南保持 80/120 个字符限制,那么即使在这种情况下您也可以遵守它们。
根据:
http://framework.zend.com/manual/en/coding-standard.coding-style.html< /a>
字符串连接(一级额外缩进)
关联数组(一级额外缩进)
以上两个组合(长字符串的两级额外缩进)
编辑:
上面的方法不起作用(感谢和抱歉 Takeshin),因为 PHP 似乎不允许在类属性的初始值中使用任何代码/运算符。
解决方案是在类构造函数中连接并设置初始值:
If you cannot store the values in a DB/file (as per adam/Peter), and you absolutely need to keep the 80/120 character limit as per Zend Framework guidelines, then you can adhere to them even in this case.
As per:
http://framework.zend.com/manual/en/coding-standard.coding-style.html
String Concatenation (one level extra indentation)
Associative Arrays (one level extra indentation)
The above two combined (two level extra indentation for long strings)
Edit:
The above does not work (thanks & sorry takeshin), as PHP does not seem to allow any code/operators in initial values of class properties.
The solution is to concatenate and set the initial values in class constructor:
从 PHP 5.4 开始,这似乎不再是问题。我有一些代码通常可以处理 5000 多个字符串,将它们作为数组条目传递,在 5.4 中没有出现问题。然而,当由于服务器更改而迁移回 5.3 时,我遇到了无法使用超长字符串定义数组的障碍。
我的问题有些不同,也许这有帮助,也许没有。如果您的应用程序需要一个数组作为其数据库参数,并且对象无法剪切它,那么您可能可以将字符串作为 SQL 语句的一部分包含进来,直到您可以进入 5.4。
编辑:作为补充说明,我们的服务器管理员根据我(和我的团队)的请求将我们升级到 5.4,事实上,它确实纠正了将长字符串保存为数组元素的问题。
As of PHP 5.4 this does not seem to be an issue. I had some code that routinely handled 5000+ character strings, passing them as array entries without issue in 5.4. However, when migrating back to 5.3 due to a server change, I encountered a roadblock where the array could not be defined with a superlong string.
My problem is somewhat different, and perhaps this is helpful, perhaps not. If your app wants an array for its DB params, and an object just won't cut it, then you might be able to include your string as part of the SQL statement instead, until you can get onto 5.4.
EDIT: As an added note, our server admin upgraded us to 5.4 per my (and my team's) request, and it did, in fact, correct the issue of holding a long string as an array element.
仅仅为了代码的可读性而将变量限制为一定的长度是没有意义的。你有什么选择?将其拆分为多个变量并连接?无意义的额外工作(对你和 php 来说)
It doesn't make sense to limit your variables to a certain length just for readability of code. What are your options? Split it into several variables and concatenate? Pointless extra work (for you and php)
亚当说的话。如果您确实不希望代码中出现长行,则始终可以将字符串放在平面文件或数据库中,并在初始化时读取它们。
What adam said. If you really don't want long lines in your code, you can always have the strings in a flat file or db, and read them in at init time.
如果字符串非常长,您始终可以使用
$var = include('file.php');
并在该文件内无论如何,我认为该规则适用于交互式线路。意味着你永远不应该像这样长:
不应该使用第一个和第二个,因为你很容易忽略变量或迷失在括号中。另一方面,第三个内部没有变量、函数或任何其他逻辑,因此您几乎不需要写入其中。您可以使用 IDE 换行...只要其他“动态和交互式”行数保持在 80 以下。
但这只是我个人的偏好,“法律上”的答案在 MicE 的帖子中。
if the string is reeeeeeealy long, you can always use
$var = include('file.php');
and inside that file<?php return "loooooong string";
Anyways I think that the rule is meant for INTERACTIVE lines. Means you should never have long like like this:
The first and second should not be used because you can easily overlook the variables or get lost in brackets. On the other hand the third has no variables, functions or any other logic inside, so there is little chance that you will ever need to write into that. And you can use you IDEs line wrap for that... as long as other "dynamic and interactive" lines are kept below 80.
But this is only my personal preference and the "de jure" answer is in the MicE's post.
如果允许换行,您可以使用 Nowdoc
但看起来很糟糕!
If line breaks are allowed you can use Nowdoc
But looks bad!