<?php
function spellingsOut( $int , $array ) { // возвращается склонение в зависимости от последних цифр
if ( $int % 10 == 1 && $int % 100 != 11 ) {
return $array [ 0 ] ;
}
if ( $int % 10 > 1 && $int % 10 < 5 ) {
return $array [ 1 ] ;
}
if ( $int % 10 == 0 || ( $int % 100 > 10 && $int % 100 < 20 ) || ( $int % 10 > 4 && $int % 10 < 10 ) ) {
return $array [ 2 ] ;
}
}
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $thousand , $million , $ruble ) {
$spellings = [
[
"тысяча" ,
"тысячи" ,
"тысяч"
] ,
[
"миллион" ,
"миллиона" ,
"миллионов"
] ,
[
"рубль" ,
"рубля" ,
"рублей"
]
] ;
if ( $thousand ) { // что отображать: миллионы, тысячи или рубли в зависимости от принятых inclineWord аргументов
return spellingsOut ( $number , $spellings [ 0 ] ) ;
} else if ( $million ) {
return spellingsOut ( $number , $spellings [ 1 ] ) ;
} else if ( $ruble ) {
return spellingsOut ( $number , $spellings [ 2 ] ) ;
}
}
/*
* Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
* если мы считаем число для мужского рода (один рубль),
* и 1 — для женского (одна тысяча)
*/
function smallNumberToText( $number , $isFemale ) {
0 => '' ,
10 => 'десять' ,
100 => 'сто' ,
1 => 'один' ,
11 => 'одиннадцать' ,
20 => 'двадцать' ,
200 => 'двести' ,
2 => 'два' ,
12 => 'двенадцать' ,
30 => 'тридцать' ,
300 => 'триста' ,
3 => 'три' ,
13 => 'тринадцать' ,
40 => 'сорок' ,
400 => 'четыреста' ,
4 => 'четыре' ,
14 => 'четырнадцать' ,
50 => 'пятьдесят' ,
500 => 'пятьсот' ,
5 => 'пять' ,
15 => 'пятнадцать' ,
60 => 'шестьдесят' ,
600 => 'шестьсот' ,
6 => 'шесть' ,
16 => 'шестнадцать' ,
70 => 'семьдесят' ,
700 => 'семьсот' ,
7 => 'семь' ,
17 => 'семнадцать' ,
80 => 'восемьдесят' ,
800 => 'восемьсот' ,
8 => 'восемь' ,
18 => 'восемнадцать' ,
90 => 'девяносто' ,
900 => 'девятьсот' ,
9 => 'девять' ,
19 => 'девятнадцать'
) ;
$femaleSpelling = array ( 1 => 'одна' ,
2 => 'две'
) ;
$words = [ ] ;
if ( $number > 99 ) { // если есть сотни
}
if ( $number % 100 > 10 && $number % 100 < 20 ) { // если есть 11-19
} else {
array_push ( $words , $spelling [ floor ( ( $number % 100 ) / 10 ) * 10 ] ) ; // десятки if ( $isFemale ) { // зависимость рода от последней цифры
array_push ( $words , $femaleSpelling [ $number % 10 ] ) ; } else
}
}
function numberToText( $number ) {
if ( $number == 0 ) {
return "ноль (0) рублей" ;
}
$words = [ ] ;
if ( $number > 999999 ) { //миллионы
array_push ( $words , smallNumberToText
( floor ( $number / 1000000 ) , 0 ) ) ; array_push ( $words , inclineWord
( floor ( $number / 1000000 ) , 0 , 1 , 0 ) ) ; }
if ( $number % 1000000 > 999 ) { //тысячи
$trimmedNumber = floor ( $number % 1000000 / 1000 ) ; //выделяем число тысяч if ( $trimmedNumber % 10 == 1 || $trimmedNumber % 10 == 2 ) { //если нужен женский род
array_push ( $words , smallNumberToText
( $trimmedNumber , 1 ) ) ; } else { //если ТНН
array_push ( $words , smallNumberToText
( $trimmedNumber , 0 ) ) ; }
array_push ( $words , inclineWord
( $trimmedNumber , 1 , 0 , 0 ) ) ; }
array_push ( $words , smallNumberToText
( $number % 1000 , 0 ) ) ; //последние 3 цифры числа array_push ( $words , inclineWord
( $number , 0 , 0 , 1 ) ) ; return $text ;
}
/* Вызовем функцию несколько раз */
$amount1 = mt_rand ( 1 , 99999999 ) ; $text1 = numberToText ( $amount1 ) ;
echo "На вашем счету {$text1} \n " ;
$amount2 = mt_rand ( 1 , 99999999 ) ; $text2 = numberToText ( $amount2 ) ;
echo "На вашем счету {$text2} \n " ;
$amount3 = mt_rand ( 1 , 99999999 ) ; $text3 = numberToText ( $amount3 ) ;
echo "На вашем счету {$text3} \n " ;
$amount4 = mt_rand ( 1 , 99999999 ) ; $text4 = numberToText ( $amount4 ) ;
echo "На вашем счету {$text4} \n " ;
PD9waHAKZXJyb3JfcmVwb3J0aW5nICggLSAxICk7Cm1iX2ludGVybmFsX2VuY29kaW5nICggInV0Zi04IiApOwoKZnVuY3Rpb24gc3BlbGxpbmdzT3V0KCRpbnQsICRhcnJheSkgeyAvLyDQstC+0LfQstGA0LDRidCw0LXRgtGB0Y8g0YHQutC70L7QvdC10L3QuNC1INCyINC30LDQstC40YHQuNC80L7RgdGC0Lgg0L7RgiDQv9C+0YHQu9C10LTQvdC40YUg0YbQuNGE0YAKCWlmICgkaW50ICUgMTAgPT0gMSAmJiAkaW50ICUgMTAwICE9IDExKSB7CgkJcmV0dXJuICRhcnJheSBbMF07Cgl9CglpZiAoJGludCAlIDEwID4gMSAmJiAkaW50ICUgMTAgPCA1KSB7CgkJcmV0dXJuICRhcnJheSBbMV07Cgl9CglpZiAoJGludCAlIDEwID09IDAgfHwgKCRpbnQgJSAxMDAgPiAxMCAmJiAkaW50ICUgMTAwIDwgMjApIHx8ICgkaW50ICUgMTAgPiA0ICYmICRpbnQgJSAxMCA8IDEwKSkgewoJCXJldHVybiAkYXJyYXkgWzJdOwoJfQp9Ci8qINCS0L7Qt9Cy0YDQsNGJ0LDQtdGCINGB0L7QvtGC0LLQtdGC0YHRgtCy0YPRjtGJ0YPRjiDRh9C40YHQu9GDINGE0L7RgNC80YMg0YHQu9C+0LLQsDogMSDRgNGD0LHQu9GMLCAyINGA0YPQsdC70Y8sIDUg0YDRg9Cx0LvQtdC5ICovCmZ1bmN0aW9uIGluY2xpbmVXb3JkKCRudW1iZXIsICR0aG91c2FuZCwgJG1pbGxpb24sICRydWJsZSkgewoJJHNwZWxsaW5ncyA9IFsgCgkJCVsgCgkJCQkJItGC0YvRgdGP0YfQsCIsCgkJCQkJItGC0YvRgdGP0YfQuCIsCgkJCQkJItGC0YvRgdGP0YciIAoJCQldLAoJCQlbIAoJCQkJCSLQvNC40LvQu9C40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