有很多问题解释如何回显单数或复数变量,但没有人回答我的问题,即如何设置变量来包含所述单数或复数值。
我原以为它会按如下方式工作:
$bottom="You have favourited <strong>$count</strong> " . $count == 1 ? 'user':'users';
但这不起作用。
有人可以告诉我如何实现上述目标吗?
最佳答案
你可以试试我写的这个函数:
/**
* Pluralizes a word if quantity is not one.
*
* @param int $quantity Number of items
* @param string $singular Singular form of word
* @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
* @return string Pluralized word if quantity is not one, otherwise singular
*/
public static function pluralize($quantity, $singular, $plural=null) {
if($quantity==1 || !strlen($singular)) return $singular;
if($plural!==null) return $plural;
$last_letter = strtolower($singular[strlen($singular)-1]);
switch($last_letter) {
case 'y':
return substr($singular,0,-1).'ies';
case 's':
return $singular.'es';
default:
return $singular.'s';
}
}
用法:
pluralize(4, 'cat'); // cats
pluralize(3, 'kitty'); // kitties
pluralize(2, 'octopus', 'octopii'); // octopii
pluralize(1, 'mouse', 'mice'); // mouse
显然有很多特殊单词该函数无法正确复数,但这就是 $plural
参数的用途:-)
看看Wikipedia看看复数有多么复杂!
关于php - 变量的单数或复数设置。 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1534127/