Skip to content

Instantly share code, notes, and snippets.

@pafnuty
Created May 25, 2015 18:36
Show Gist options
  • Save pafnuty/d623ae51f7d34c457165 to your computer and use it in GitHub Desktop.
Save pafnuty/d623ae51f7d34c457165 to your computer and use it in GitHub Desktop.
<?
/**
* Функция для склонения слов
* Пример использования: declination(5,'комментари|й|я|ев')
*
*
* @param int n число, для которого будет расчитано окончание
* @param string words слово и варианты окончаний для 1|2|1 (1 комментарий, 2 комментария, 100 комментариев)
*
* @return string - слово с правильным окончанием
*/
function declination($n = 0, $words) {
$words = explode('|', $words);
$n = intval($n);
return $n % 10 == 1 && $n % 100 != 11 ? $words[0] . $words[1] : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? $words[0] . $words[2] : $words[0] . $words[3]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment