Created
August 26, 2016 09:58
-
-
Save s2ar/1d280d90d885799e543029b8e2250544 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
<?php | |
/** | |
* Преобразование количества "цифрой" в количество "словом" | |
* Чтобы было удобнее формировать массивы со склонениями, запомните ряд чисел 1-2-5, | |
* а потом мысленно подставляйте их в массив: (один "рубль", два "рубля", пять "рублей") | |
* $num = 3; | |
* $words = array('новость', 'новости', 'новостей'); | |
* echo $num . ' ' . num2word($num, $words); // сколько новостей | |
* | |
* @param $n | |
* @param $words | |
* | |
* @return mixed | |
* @link http://www.manhunter.ru/webmaster/15_seychas_na_sayte_2_gostey_i_3_novostey.html | |
*/ | |
function num2word($n, $words) | |
{ | |
return ($words[($n = ($n = $n % 100) > 19 ? ($n % 10) : $n) == 1 ? 0 : (($n > 1 && $n <= 4) ? 1 : 2)]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment