<?php
// Staring straight up into the sky ... oh my my
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $word1 , $word2 , $word5 ) {
$test = $number % 100 ;
if ( $test > 20 ) {
$test = $test % 10 ;
}
if ( $test == 0 && $number != 0 ) {
return $word5 ;
} elseif ( $test == 1 ) {
return $word1 ;
} elseif ( $test >= 2 && $test <= 4 ) {
return $word2 ;
} elseif ( $test >= 5 && $test <= 20 ) {
return $word5 ;
} else {
return $word5 ;
}
}
/*
Преобразует числа от 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 => 'две'
) ;
$numberStr = "" ;
$hundred = 0 ; // сотни
$tens = 0 ; // десятки
$units = 0 ; // единицы
if ( $isFemale == 1 ) {
if ( isset ( $femaleSpelling [ $number ] ) ) { return $femaleSpelling [ $number ] ;
}
}
if ( isset ( $spelling [ $number ] ) ) { $numberStr = $spelling [ $number ] ;
} else {
$hundred = floor ( $number / 100 ) ; $tens = $number % 100 ;
$units = $number % 10 ;
if ( $hundred != 0 ) {
$numberStr = $spelling [ $hundred * 100 ] . " " ;
}
if ( isset ( $spelling [ $tens ] ) ) { if ( isset ( $femaleSpelling [ $tens ] ) && $isFemale == 1 ) { $numberStr = $numberStr . $femaleSpelling [ $tens ] ;
} else {
$numberStr = $numberStr . $spelling [ $tens ] ;
}
} else {
$numberStr = $numberStr . $spelling [ $tens - $units ] ;
if ( isset ( $femaleSpelling [ $units ] ) && $isFemale == 1 ) { $numberStr = $numberStr . " " . $femaleSpelling [ $units ] ;
} else {
$numberStr = $numberStr . " " . $spelling [ $units ] ;
}
}
}
return $numberStr ;
}
function numberToText( $number ) {
$numericDigit = 0 ;
$indexName = 0 ;
$dischargeCursiveScript = "" ;
$result = "" ;
$word = "" ;
$saveNumber = 0 ;
if ( $number > PHP_INT_MAX) {
return "Число слишком большое" ;
} elseif ( $number == 0 ) {
return "ноль (0) рублей" ;
}
$largeNumbersNames = array ( array ( 'рубль' , 'рубля' , 'рублей' ) , array ( 'тысяча' , 'тысячи' , 'тысяч' ) , array ( 'миллион' , 'миллиона' , 'миллионов' ) , array ( 'миллиард' , 'миллиарда' , 'миллиардов' ) //array('триллион', 'триллиона', 'триллионов'),
//array('квадриллион', 'квадриллиона', 'квадриллионов')
) ;
$saveNumber = $number ;
$indexName = 0 ;
while ( $number != 0 ) {
$numericDigit = $number % 1000 ;
if ( $numericDigit != 0 ) {
$dischargeCursiveScript = smallNumberToText( $numericDigit , $indexName == 1 ? 1 : 0 ) ;
$word = inclineWord( $numericDigit , $largeNumbersNames [ $indexName ] [ 0 ] , $largeNumbersNames [ $indexName ] [ 1 ] , $largeNumbersNames [ $indexName ] [ 2 ] ) ;
if ( $indexName == 0 ) {
$word = "(" . $saveNumber . ") " . $word ;
}
$dischargeCursiveScript = $dischargeCursiveScript . " " . $word ;
$result = $dischargeCursiveScript . " " . $result ;
} elseif ( $indexName == 0 ) {
$word = inclineWord( $numericDigit , $largeNumbersNames [ $indexName ] [ 0 ] , $largeNumbersNames [ $indexName ] [ 1 ] , $largeNumbersNames [ $indexName ] [ 2 ] ) ;
$result = "(" . $saveNumber . ") " . $word ;
}
$number = ( $number - $numericDigit ) / 1000 ;
$indexName ++;
}
return $result ;
}
/* Вызовем функцию несколько раз */
$text1 = numberToText( $amount1 ) ;
echo "На вашем счету: {$text1} \n " ;
$text2 = numberToText( $amount2 ) ;
echo "На вашем счету: {$text2} \n " ;
$text3 = numberToText( $amount3 ) ;
echo "На вашем счету: {$text3} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету: {$text4} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету: {$text4} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету: {$text4} \n " ;
?>
PD9waHAKCS8vIFN0YXJpbmcgc3RyYWlnaHQgdXAgaW50byB0aGUgc2t5IC4uLiBvaCBteSBteQoJZXJyb3JfcmVwb3J0aW5nKC0xKTsKCW1iX2ludGVybmFsX2VuY29kaW5nKCd1dGYtOCcpOwoKCS8qINCS0L7Qt9Cy0YDQsNGJ0LDQtdGCINGB0L7QvtGC0LLQtdGC0YHRgtCy0YPRjtGJ0YPRjiDRh9C40YHQu9GDINGE0L7RgNC80YMg0YHQu9C+0LLQsDogMSDRgNGD0LHQu9GMLCAyINGA0YPQsdC70Y8sIDUg0YDRg9Cx0LvQtdC5ICovCglmdW5jdGlvbiBpbmNsaW5lV29yZCgkbnVtYmVyLCAkd29yZDEsICR3b3JkMiwgJHdvcmQ1KSB7CgkJJHRlc3QgPSAkbnVtYmVyJTEwMDsKCQlpZigkdGVzdD4yMCl7CgkJCSR0ZXN0ID0gJHRlc3QlMTA7CgkJfQoJCWlmKCR0ZXN0PT0wICYmICRudW1iZXIgIT0gMCl7CgkJCXJldHVybiAkd29yZDU7CgkJfWVsc2VpZigkdGVzdD09MSl7CgkJCXJldHVybiAkd29yZDE7CgkJfWVsc2VpZigkdGVzdD49MiAmJiAkdGVzdDw9NCl7C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