Skip to content

Instantly share code, notes, and snippets.

@Neolot
Last active September 19, 2023 12:52
Show Gist options
  • Save Neolot/3964380 to your computer and use it in GitHub Desktop.
Save Neolot/3964380 to your computer and use it in GitHub Desktop.
PHP Склонение числительных
<?php
/**
* Функция склонения числительных в русском языке
*
* @param int $number Число которое нужно просклонять
* @param array $titles Массив слов для склонения
* @return string
**/
$titles = array('Сидит %d котик', 'Сидят %d котика', 'Сидит %d котиков');
function declOfNum($number, $titles)
{
$cases = array (2, 0, 1, 1, 1, 2);
$format = $titles[ ($number%100 > 4 && $number %100 < 20) ? 2 : $cases[min($number%10, 5)] ];
return sprintf($format, $number);
}
@Cellard
Copy link

Cellard commented Dec 28, 2018

$titles = array('Сидит %d котик', 'Сидят %d котика', 'Сидит %d котиков');
function declOfNum($number, $titles)
{
    $cases = array (2, 0, 1, 1, 1, 2);
    $format = $titles[ ($number%100 > 4 && $number %100 < 20) ? 2 : $cases[min($number%10, 5)] ];
    return sprintf($format, $number);
}

@Neolot
Copy link
Author

Neolot commented Dec 17, 2019

Согласен, так лучше, внес изменения в код.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment