<?php
// Staring straight up into the sky ... oh my my
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $word1 , $word2 , $word5 ) {
$last2Digits = $number % 100 ;
if ( preg_match ( "/^1$|21|31|41|51|61|71|81|91/" , $last2Digits ) ) { if ( preg_match ( "/^1$|21|31|41|51|61|71|81|91/" , $last2Digits ) && $word1 == "тысяча" ) { $numberEnd = smallNumberToText
( $number , 1 ) . " {$word1} " ; } else { $numberEnd = smallNumberToText( $number , 0 ) . " {$word1} " ; }
}
else if ( preg_match ( "/^[2-4]$|2[2-4]|3[2-4]|4[2-4]|5[2-4]|6[2-4]|7[2-4]|8[2-4]|9[2-4]/" , $last2Digits ) ) {
if ( preg_match ( "/^2$|22|32|42|52|62|72|82|92/" , $last2Digits ) && $word2 == "тысячи" ) { $numberEnd = smallNumberToText
( $number , 1 ) . " {$word2} " ; } else { $numberEnd = smallNumberToText( $number , 0 ) . " {$word2} " ; }
}
else if ( preg_match ( "/^[5-9]$|0|[1-9]0|1[0-9]|2[5-9]|3[5-9]|4[5-9]|5[5-9]|6[5-9]|7[5-9]|8[5-9]|9[5-9]/" , $last2Digits ) ) {
$numberEnd = smallNumberToText( $number , 0 ) . " {$word5} " ;
}
return $numberEnd ;
}
/*
Преобразует числа от 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 => 'девятнадцать'
) ;
1 => 'одна' , 2 => 'две'
) ;
$thousand = floor ( $number / 100 ) * 100 ;
if ( ( $number - $thousand ) < 10 || ( $number - $thousand ) > 19 ) {
$ten = floor ( ( $number - $thousand ) / 10 ) * 10 ; } else {
$ten = $number - $thousand ;
}
$oneUnit = $number - $thousand - $ten ;
if ( $thousand == 0 ) {
$thousandText = "" ;
} else {
$thousandText = $spelling [ $thousand ] ;
}
if ( $ten == 0 ) {
$tenText = "" ;
} else {
$tenText = $spelling [ $ten ] ;
}
if ( $oneUnit == 0 ) {
$oneUnitText = "" ;
} else {
if ( $isFemale == 1 ) {
$oneUnitText = $femaleSpelling [ $oneUnit ] ;
} else {
$oneUnitText = $spelling [ $oneUnit ] ;
}
}
return $thousandText . " " . $tenText . " " . $oneUnitText ;
}
function numberToText( $number ) {
$millionsText = "" ;
$thousandsText = "" ;
$hundredsText = "" ;
if ( $number == 0 ) {
return "ноль рублей" ;
}
$millions = floor ( $number / 1000000 ) ; if ( $millions != 0 ) {
$millionsText = inclineWord( $millions , "миллион" , "миллиона" , "миллионов" ) ;
}
$thousands = floor ( ( $number - $millions * 1000000 ) / 1000 ) ; if ( $thousands != 0 ) {
$thousandsText = inclineWord( $thousands , "тысяча" , "тысячи" , "тысячь" ) ;
}
$hundreds = $number - ( $millions * 1000000 ) - ( $thousands * 1000 ) ;
if ( $hundreds != 0 ) {
$hundredsText = inclineWord( $hundreds , "рубль" , "рубля" , "рублей" ) ;
}
return "{$millionsText} {$thousandsText} {$hundredsText} " ;
}
/* Вызовем функцию несколько раз */
$text1 = numberToText( $amount1 ) ;
echo "На вашем счету {$text1} \n " ;
$text2 = numberToText( $amount2 ) ;
echo "На вашем счету {$text2} \n " ;
$text3 = numberToText( $amount3 ) ;
echo "На вашем счету {$text3} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету {$text4} \n " ;
PD9waHAKIAovLyBTdGFyaW5nIHN0cmFpZ2h0IHVwIGludG8gdGhlIHNreSAuLi4gb2ggbXkgbXkKZXJyb3JfcmVwb3J0aW5nKC0xKTsKbWJfaW50ZXJuYWxfZW5jb2RpbmcoJ3V0Zi04Jyk7CiAKIAovKiDQktC+0LfQstGA0LDRidCw0LXRgiDRgdC+0L7RgtCy0LXRgtGB0YLQstGD0Y7RidGD0Y4g0YfQuNGB0LvRgyDRhNC+0YDQvNGDINGB0LvQvtCy0LA6IDEg0YDRg9Cx0LvRjCwgMiDRgNGD0LHQu9GPLCA1INGA0YPQsdC70LXQuSAqLwpmdW5jdGlvbiBpbmNsaW5lV29yZCgkbnVtYmVyLCAkd29yZDEsICR3b3JkMiwgJHdvcmQ1KSB7CgokbGFzdDJEaWdpdHMgPSAkbnVtYmVyICUgMTAwOwoKaWYocHJlZ19tYXRjaCgiL14xJHwyMXwzMXw0MXw1MXw2MXw3MXw4MXw5MS8iLCRsYXN0MkRpZ2l0cykpewogIGlmKHByZWdfbWF0Y2goIi9eMSR8MjF8MzF8NDF8NTF8NjF8NzF8ODF8OTEvIiwkbGFzdDJEaWdpdHMpICYmICR3b3JkMT09ItGC0YvRgdGP0YfQsCIpeyAkbnVtYmVyRW5kID0gc21hbGxOdW1iZXJUb1RleHQoJG51bWJlciwxKS4iIHskd29yZDF9Ijt9CiAgZWxzZXskbnVtYmVyRW5kID0gc21hbGxOdW1iZXJUb1RleHQoJG51bWJlciwwKS4iIHskd29yZDF9Ijt9Cn0KZWxzZSBpZihwcmVnX21hdGNoKCIvXlsyLTRdJHwyWzItNF18M1syLTRdfDRbMi00XXw1WzItNF18NlsyLTRdfDdbMi00XXw4WzItNF18OVsyLTRdLyIsJGxhc3QyRGlnaXRzKSkKewogIGlmKHBy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