Last active
January 12, 2022 12:30
-
-
Save artikus11/e9453bcf757df7b48697e56d2d1bed04 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
/* | |
* Пример использования | |
*/ | |
$counts = [ 1, 2, 7 ]; | |
foreach ( $counts as $count ) { | |
echo $count . ' - ' . get_num_ending( $count, [ 'машина', 'машины', 'машин' ] ) . '<br>'; | |
} | |
/* | |
// Выведет | |
1 - машина | |
2 - машины | |
7 - машин | |
*/ |
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
/** | |
* Форматирование окончаний слов | |
* | |
* @param int $number Число на основе которого нужно сформировать окончание | |
* @param array $ending_array Массив слов или окончаний для чисел (1, 4, 5), | |
* например array('яблоко', 'яблока', 'яблок') | |
* | |
* @return string | |
* | |
* @author unknown | |
* @verphp 7.0 | |
*/ | |
function get_num_ending( int $number, array $ending_array ): string { | |
$number %= 100; | |
if ( $number >= 11 && $number <= 19 ) { | |
$ending = $ending_array[2]; | |
} else { | |
$i = $number % 10; | |
switch ( $i ) { | |
case ( 1 ): | |
$ending = $ending_array[0]; | |
break; | |
case ( 2 ): | |
case ( 3 ): | |
case ( 4 ): | |
$ending = $ending_array[1]; | |
break; | |
default: | |
$ending = $ending_array[2]; | |
} | |
} | |
return $ending; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment