Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- switch((string)$count{позиции_последних_двух_символов}) {
- case
- ...
- }
- function declension($digit,$expr,$onlyword=false)
- {
- if(!is_array($expr)) $expr = array_filter(explode(' ', $expr));
- if(empty($expr[2])) $expr[2]=$expr[1];
- $i=preg_replace('/[^0-9]+/s','',$digit)%100; //intval не всегда корректно работает
- if($onlyword) $digit='';
- if($i>=5 && $i<=20) $res=$digit.' '.$expr[2];
- else
- {
- $i%=10;
- if($i==1) $res=$digit.' '.$expr[0];
- elseif($i>=2 && $i<=4) $res=$digit.' '.$expr[1];
- else $res=$digit.' '.$expr[2];
- }
- return trim($res);
- }
- echo 'Мне уже '.declension('<b>20</b>','год года лет').'!';
- function declension($n, $words) {
- is_array($words) or $words = array_slice(func_get_args(), 1, 3);
- 19 < $n = abs($n) % 100 and $n %= 10;
- if ($n == 1) {
- return reset($words);
- }
- if ($n && $n < 5) {
- return $words[min(1, count($words) - 1)];
- }
- return end($words);
- }
- echo sprintf(
- "Мне %u %s. I'm Jesus!",
- $count = date('Y'),
- declension($count, 'год', 'года', 'лет')
- ), PHP_EOL;
Add Comment
Please, Sign In to add comment