Skip to content

Instantly share code, notes, and snippets.

@Londeren
Last active August 17, 2022 12:54
Show Gist options
  • Save Londeren/a2e37e39cf22446818d2 to your computer and use it in GitHub Desktop.
Save Londeren/a2e37e39cf22446818d2 to your computer and use it in GitHub Desktop.
Склоняет существительное в зависимости от числительного идущего перед ним.
<?php
/**
* Склоняет существительное в зависимости от числительного идущего перед ним.
* Пример использования:
* 1. pluralForm($n, "письмо", "письма", "писем", 'письма отсутствуют')
* 2. pluralForm($n, array("письмо", "письма", "писем", 'письма отсутствуют'));
* @param $n int|float число
* @param $normative string|array Именительный падеж слова ИЛИ массив
* @param $singular string Родительный падеж ед. число
* @param $plural string Множественное число
* @param $zero string форма слова, если $n - ноль, optional
* @return string
*/
function pluralForm($n, $normative, $singular = null, $plural = null, $zero = null)
{
// массив существительных
if (is_array($normative))
{
if($n == 0 && count($normative) == 4)
return end($normative);
$cases = array(2, 0, 1, 1, 1, 2);
return $normative[($n % 100 > 4 && $n % 100 < 20) ? 2 : $cases[min($n % 10, 5)]];
}
if ($n == 0 && !is_null($zero))
return $zero;
$n = abs($n) % 100;
$n1 = $n % 10;
if ($n > 10 && $n < 20) return $plural;
if ($n1 > 1 && $n1 < 5) return $singular;
if ($n1 == 1) return $normative;
return $plural;
}
/**
* Plural forms for russian words
* @param {Integer} count quantity for word
* @param {Array} words Array of words. Example: ['письмо', 'письма', 'писем']
* @return {String} Count + plural form for word
*/
function pluralize(count, words) {
var cases = [2, 0, 1, 1, 1, 2];
return count + ' ' + words[ (count % 100 > 4 && count % 100 < 20) ? 2 : cases[ Math.min(count % 10, 5)] ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment