Detecting number-related modificators when counting nouns in russian language (Определение числа и склонения существительного по количеству)

<?php
 
/*
 * When counting in russian, noun endings change depending on number in use.
 * There are 3 distinct groups of numbers that affect the case and plural form
 * of a noun. Unfortunately, they don't have any specific names in textbooks,
 * so I'll have to call them Group 0, Group 1 and Group 2.
 *
 * The basic rules are:
 *  - numbers ending in "-1" (but not "11") select Group 0 ("nominative singular")
 *  - numbers ending in "-2", "-3", "-4" (but not "12", "13", "14") select 
 *    Group 1 ("genitive singular")
 *  - numbers ending in "-5", "-6", "-7", "-8", "-9" and the 'teen' numbers "11",
 *    "12", "13", "14" select Group 2 ("genitive plural")
 *
 * This function takes a number and returns the identified group.
 * It's easy to spot that you can extract the modificators out of the
 * returned value in this way: 
 *  (ret < 2 ? "singular" : "plural")      - plural[ization]
 *  (ret > 0 ? "genitive" : "nominative")  - case [declination]
 */
function ru_number_case($number) {
	return ((substr("0".$number, -2, 1) == '1' || 
		(($l1 = substr($number, -1)) >= 5 && $l1 <= 9) || $l1 == '0') ? 2 :
		(	($l1 >= 2 && $l1 <= 4) ? 1 : 0	));
}
 
php?>
for ($i = 0; $i < 10; $i++) {
	$n = round(rand(0,300));
	$g = ru_number_case($n);
	echo $n . " - " .
		($g > 0 ? "genitive" : "nominative") . " " . 
		($g < 2 ? "singular" : "plural") . "<BR>";
}
 
49 - genitive plural
236 - genitive plural
225 - genitive plural
199 - genitive plural
110 - genitive plural
100 - genitive plural
254 - genitive singular
275 - genitive plural
141 - nominative singular
281 - nominative singular