变量的单数或复数设置。 PHP
有很多问题解释如何回显单数或复数变量,但没有一个问题回答我的问题,即如何设置变量以包含所述单数或复数值。
我本以为它会按如下方式工作:
$bottom="You have favourited <strong>$count</strong> " . $count == 1 ? 'user':'users';
但这不起作用。
有人可以建议我如何实现上述目标吗?
There are a lot of questions explaining how to echo a singular or plural variable, but none answer my question as to how one sets a variable to contain said singular or plural value.
I would have thought it would work as follows:
$bottom="You have favourited <strong>$count</strong> " . $count == 1 ? 'user':'users';
This however does not work.
Can someone advise how I achieve the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
你可以尝试我写的这个函数:
用法:
显然有很多特殊的单词,这个函数不能正确地复数,但这就是
$plural
参数的用途:-)看一下 维基百科看看复数有多么复杂!
You can try this function I wrote:
Usage:
There's obviously a lot of exceptional words that this function will not pluralize correctly, but that's what the
$plural
argument is for :-)Take a look at Wikipedia to see just how complicated pluralizing is!
您可能想查看 gettext 扩展。更具体地说,听起来
ngettext()
会做你想做的事:只要你有一个数字可供计数,它就会正确地复数单词。您还可以使其正确处理翻译后的复数形式,这是它的主要目的,尽管需要做很多额外的工作。
You might want to look at the gettext extension. More specifically, it sounds like
ngettext()
will do what you want: it pluralises words correctly as long as you have a number to count from.You can also make it handle translated plural forms correctly, which is its main purpose, though it's quite a lot of extra work to do.
IMO 的最佳方法是为每种语言提供一个包含所有复数规则的数组,即
array('man'=>'men', 'woman'=>'women');
和为每个单数单词编写一个pluralize()函数。您可能想看看 CakePHP 变形器以获得一些灵感。
https://github.com/cakephp/cakephp/blob/master /src/Utility/Inflector.php
The best way IMO is to have an array of all your pluralization rules for each language, i.e.
array('man'=>'men', 'woman'=>'women');
and write a pluralize() function for each singular word.You may want to take a look at the CakePHP inflector for some inspiration.
https://github.com/cakephp/cakephp/blob/master/src/Utility/Inflector.php
这将解决您的问题,感谢 mario 和 三元运算符和字符串连接怪癖?
This will solve your issue, thanks to mario and Ternary operator and string concatenation quirk?
享受:https://github.com/ICanBoogie/Inflector
Enjoy: https://github.com/ICanBoogie/Inflector
如果您打算编写自己的复数函数,那么您可能会发现复数的算法描述很有帮助:
http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
或者更简单的方法可能是使用 Internet 上提供的现成复数函数之一:
http://www.eval.ca/2007/03/03/php-pluralize-method/If you're going to go down the route of writing your own pluralize function then you might find this algorithmic description of pluralisation helpful:
http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
Or the much easier approach would probably be to use one of the ready-made pluralize functions available on the Internet:
http://www.eval.ca/2007/03/03/php-pluralize-method/对于
$count = 1
:PHP 解析器(正确地)假设问号左边的所有内容都是条件,除非您通过添加自己的括号来更改优先顺序(如其他答案)。
For
$count = 1
:The PHP parser (rightly) assumes everything to the left of the question mark is the condition, unless you change the order of precedence by adding in parenthesis of your own (as stated in other answers).
定制、透明且免扩展的解决方案。
不确定它的速度。
乌克兰语/俄语用法:
Custom, transparent and extension-free solution.
Not sure about its speed.
Usage in Ukrainian / Russian:
您可以尝试使用
$count < 2
因为$count
也可以是0
输出
You can try using
$count < 2
because$count
can also be0
Output
这是一种方法。
This is one way to do it.
在 laravel 中,Str 类具有复数辅助函数:
或者您可以使用 Pluralizer 类:
In laravel, Str class has plural helper funcion:
Or you may use Pluralizer class: