Skip to content

Instantly share code, notes, and snippets.

@aclips
Last active April 26, 2022 07:46
Show Gist options
  • Save aclips/1ca4a0dc27392ad42119c2d1875a2863 to your computer and use it in GitHub Desktop.
Save aclips/1ca4a0dc27392ad42119c2d1875a2863 to your computer and use it in GitHub Desktop.
Склонение существительных после числительных
<?php
/**
* Склонение существительных после числительных.
*
* @param string $value Значение
* @param array $words Массив вариантов, например: array('товар', 'товара', 'товаров')
* @param bool $show Включает значение $value в результирующею строку
* @return string
*/
public function num_word($value, $words, $show = true)
{
$num = $value % 100;
if ($num > 19) {
$num = $num % 10;
}
$out = ($show) ? $value . ' ' : '';
switch ($num) {
case 1:
$out .= $words[0];
break;
case 2:
case 3:
case 4:
$out .= $words[1];
break;
default:
$out .= $words[2];
break;
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment