变量的单数或复数设置。 PHP

发布于 2024-08-07 05:21:26 字数 249 浏览 15 评论 0原文

有很多问题解释如何回显单数或复数变量,但没有一个问题回答我的问题,即如何设置变量以包含所述单数或复数值。

我本以为它会按如下方式工作:

$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 技术交流群。

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

发布评论

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

评论(12

不必你懂 2024-08-14 05:21:26

你可以尝试我写的这个函数:

/**
 * 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 参数的用途:-)

看一下 维基百科看看复数有多么复杂!

You can try this function I wrote:

/**
 * 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';
    }
}

Usage:

pluralize(4, 'cat'); // cats
pluralize(3, 'kitty'); // kitties
pluralize(2, 'octopus', 'octopii'); // octopii
pluralize(1, 'mouse', 'mice'); // mouse

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!

南…巷孤猫 2024-08-14 05:21:26

您可能想查看 gettext 扩展。更具体地说,听起来 ngettext() 会做你想做的事:只要你有一个数字可供计数,它就会正确地复数单词。

print ngettext('odor', 'odors', 1); // prints "odor"
print ngettext('odor', 'odors', 4); // prints "odors"
print ngettext('%d cat', '%d cats', 4); // prints "4 cats"

您还可以使其正确处理翻译后的复数形式,这是它的主要目的,尽管需要做很多额外的工作。

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.

print ngettext('odor', 'odors', 1); // prints "odor"
print ngettext('odor', 'odors', 4); // prints "odors"
print ngettext('%d cat', '%d cats', 4); // prints "4 cats"

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.

手长情犹 2024-08-14 05:21:26

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

皇甫轩 2024-08-14 05:21:26

这将解决您的问题,感谢 mario 和 三元运算符和字符串连接怪癖?

$bottom = "You have favourited <strong>$count</strong> " . ($count == 1 ? 'user':'users');

This will solve your issue, thanks to mario and Ternary operator and string concatenation quirk?

$bottom = "You have favourited <strong>$count</strong> " . ($count == 1 ? 'user':'users');
花心好男孩 2024-08-14 05:21:26

享受:https://github.com/ICanBoogie/Inflector

多语言屈折器,将单词从单数转换为复数,
下划线改为驼峰式大小写,等等。

Enjoy: https://github.com/ICanBoogie/Inflector

Multilingual inflector that transforms words from singular to plural,
underscore to camel case, and more.

静谧幽蓝 2024-08-14 05:21:26

如果您打算编写自己的复数函数,那么您可能会发现复数的算法描述很有帮助:

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/

昨迟人 2024-08-14 05:21:26

对于 $count = 1

    "You have favourited <strong>$count</strong> " . $count == 1 ? 'user' : 'users';
=>               "You have favourited <strong>1</strong> 1" == 1 ? 'user' : 'users';
=>                                                        1 == 1 ? 'user' : 'users';
=>                                                          true ? 'user' : 'users';
// output: 'user'

PHP 解析器(正确地)假设问号左边的所有内容都是条件,除非您通过添加自己的括号来更改优先顺序(如其他答案)。

For $count = 1:

    "You have favourited <strong>$count</strong> " . $count == 1 ? 'user' : 'users';
=>               "You have favourited <strong>1</strong> 1" == 1 ? 'user' : 'users';
=>                                                        1 == 1 ? 'user' : 'users';
=>                                                          true ? 'user' : 'users';
// output: 'user'

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).

泪痕残 2024-08-14 05:21:26

定制、透明且免扩展的解决方案。
不确定它的速度。

/**
 * Custom plural
 */
function splur($n,$t1,$t2,$t3) {
    settype($n,'string');
    $e1=substr($n,-2);
    if($e1>10 && $e1<20) { return $n.' '.$t3; } // "Teen" forms
    $e2=substr($n,-1);
    switch($e2) {
        case '1': return $n.' '.$t1; break;
        case '2': 
        case '3':
        case '4': return $n.' '.$t2; break;
        default:  return $n.' '.$t3; break;
    }
}

乌克兰语/俄语用法:

splur(5,'сторінка','сторінки','сторінок') // 5 сторінок
splur(4,'сторінка','сторінки','сторінок') // 4 сторінки
splur(1,'сторінка','сторінки','сторінок') // 1 сторінка
splur(12,'сторінка','сторінки','сторінок') // 12 сторінок

splur(5,'страница','страницы','страниц') // 5 страниц
splur(4,'страница','страницы','страниц') // 4 страницы
splur(1,'страница','страницы','страниц') // 1 страница
splur(12,'страница','страницы','страниц') // 12 страниц

Custom, transparent and extension-free solution.
Not sure about its speed.

/**
 * Custom plural
 */
function splur($n,$t1,$t2,$t3) {
    settype($n,'string');
    $e1=substr($n,-2);
    if($e1>10 && $e1<20) { return $n.' '.$t3; } // "Teen" forms
    $e2=substr($n,-1);
    switch($e2) {
        case '1': return $n.' '.$t1; break;
        case '2': 
        case '3':
        case '4': return $n.' '.$t2; break;
        default:  return $n.' '.$t3; break;
    }
}

Usage in Ukrainian / Russian:

splur(5,'сторінка','сторінки','сторінок') // 5 сторінок
splur(4,'сторінка','сторінки','сторінок') // 4 сторінки
splur(1,'сторінка','сторінки','сторінок') // 1 сторінка
splur(12,'сторінка','сторінки','сторінок') // 12 сторінок

splur(5,'страница','страницы','страниц') // 5 страниц
splur(4,'страница','страницы','страниц') // 4 страницы
splur(1,'страница','страницы','страниц') // 1 страница
splur(12,'страница','страницы','страниц') // 12 страниц
可遇━不可求 2024-08-14 05:21:26

您可以尝试使用 $count < 2 因为 $count 也可以是 0

$count =1;
$bottom = sprintf("You have favourited <strong>%d %s</strong>", $count, ($count < 2 ? 'user' : 'users'));
print($bottom);

输出

You have favourited 1 user

You can try using $count < 2 because $count can also be 0

$count =1;
$bottom = sprintf("You have favourited <strong>%d %s</strong>", $count, ($count < 2 ? 'user' : 'users'));
print($bottom);

Output

You have favourited 1 user
撩动你心 2024-08-14 05:21:26

这是一种方法。

$usersText = $count == 1 ? "user" : "users";
$bottom = "You have favourited <strong>" . $count . "</strong> " , $usersText;

This is one way to do it.

$usersText = $count == 1 ? "user" : "users";
$bottom = "You have favourited <strong>" . $count . "</strong> " , $usersText;
最丧也最甜 2024-08-14 05:21:26

在 laravel 中,Str 类具有复数辅助函数:

use Str;
Str::plural('str');

或者您可以使用 Pluralizer 类:

use Illuminate\Support\Pluralizer;
Pluralizer::plural('str')

In laravel, Str class has plural helper funcion:

use Str;
Str::plural('str');

Or you may use Pluralizer class:

use Illuminate\Support\Pluralizer;
Pluralizer::plural('str')
回眸一遍 2024-08-14 05:21:26
  /**
 * Function that will return the correct word related to quantity.
 * @param type Int $quantity
 * @param type String $singular
 * @param type String $plural
 * @param type Boolean $showQuantity
 * @return type String
 */
function pluralize( $quantity, $singular, $plural, $showQuantity=true ) {
    return ( $showQuantity ? $quantity." " : "" ) . ( $quantity > 1 ? $plural : $singular );
}


pluralize(3,'função','funções');
pluralize(1,'função','funções', false);
  /**
 * Function that will return the correct word related to quantity.
 * @param type Int $quantity
 * @param type String $singular
 * @param type String $plural
 * @param type Boolean $showQuantity
 * @return type String
 */
function pluralize( $quantity, $singular, $plural, $showQuantity=true ) {
    return ( $showQuantity ? $quantity." " : "" ) . ( $quantity > 1 ? $plural : $singular );
}


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