Created
May 25, 2015 18:36
-
-
Save pafnuty/d623ae51f7d34c457165 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* Функция для склонения слов | |
* Пример использования: 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