Home php Word decline in PHP

Word decline in PHP

Author

Date

Category

There is such a small statistics

“n” people ask now help!

if (n == 1) = & gt; "1 person asks for now help"
elseif ($ n == 2) = & gt; "2 people are asking for help now"

and so on.

How to do it?


1, Authority 100%

function declofnum ($ num, $ titles) {
  $ Cases = Array (2, 0, 1, 1, 1, 2);
  Return $ num. "". $ Titles [($ num% 100 & gt; 4 & amp; & amp; $ num% 100 & lt; 20)? 2: $ Cases [min ($ num% 10, 5)]];
}
Echo Declofnum (5, Array ('man asks', 'a person is asked', 'man ask'));

2, Authority 35%

Here people update the rules for different languages: Language Pral Rules .

For Russian Language:

one → n mod 10 is 1 and n mod 100 is not 11;
FEW → N MOD 10 in 2..4 and n mod 100 not in 12..14;
Many → N MOD 10 IS 0 or N mod 10 in 5..9 or n mod 100 in 11..14;
Other → Everything ELSE

In this way:

function pluralcategory ($ count)
{
 $ MOD10 = $ Count% 10;
 $ mod100 = $ Count% 100;
 If (IS_INT ($ Count) & amp; & amp; $ MOD10 == 1 & amp; & amp; $ mod100! = 11) {
  Return 'One';
 } elseif (($ MOD10 & GT; 1 & amp; & amp; $ MOD10 & LT; 5) & amp; & amp; ($ MOD100 & LT; 12 || $ MOD100 & GT; 14)) {
  Return 'FEW';
 } elseif ($ MOD10 == 0 || ($ MOD10 & GT; 4 & amp; & amp; $ MOD10 & LT; 10) || ($ MOD100 & GT; 10 & amp; & amp; $ mod100 & lt; 15)) {
  Return 'Many';
 } else {
  Return 'Other';
 }
}

by itself, only the category will be in response. For the word “man” array of correspondences will be:

one → man
FEW → man
Many → Man
Other → Human

Demo:

$ WORD = Array (
 'one' = & gt; 'human',
 'FEW' = & GT; 'man',
 'Many' = & gt; 'human',
 'other' = & gt; 'man',
);
for ($ i = 1; $ i & lt; 500; $ i ++) {
 Echo $ i. ' '. $ Word [PluralCategory ($ i)].' & lt; br / & gt; ';
}

3, Authority 26%

Option for this case is excessable, but it is suitable for other cases when the word is not two, but three forms:

for ($ n = 1; $ n & lt; 4096; $ n ++)
{
  $ plural =.
  (
    $ n% 10 == 1 & amp; & amp; $ n% 100! = 11
    .
    'human'
    :
    (
      $ n% 10 & gt; = 2 & amp; & amp; $ n% 10 & lt; = 4 & amp; & amp;
      (
        $ n% 100 & lt; 10 || $ n% 100 & gt; = 20
      )
      .
      'man'
      :
      'human'
    )
  );
  Print "$ n $ plural \ n";
}

4, Authority 23%

Remarking too. Crossed the algorithm @Lampa with your templates. It seems to me so more convenient. Code for many lines due to a free recording of a regular expression, so just give a reference to the implementation with tests:
http://ideone.com/xqgk2g
The Sklonit function you need to simply pass the text containing how much templates:

// template 1
[Number: Start (end1, end2, end3)] // Start + end
[2: Apples (Oh, A,)] // Apples
// Template 2.
Number [Separator: Start (end1, end2, end3)] // Number + Separator + Start + End
2 [: apples (oh, a,)] // 2 apples

At the same time, if the separator in the second template is a number, then it is considered that this is the template of the first type, that is,

1 [2: apples (o, a,)] // 1Block, and not 12Ablok, if you perceive it like Template 2.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions