<?php
/**
* 05.05.2015 (20:17)
* TheNumbers.php
* PhpStorm
*/
header ( "Content-Type: text/plain; charset=utf-8" ) ;
/* Делает первую букву предложения заглавной */
function makeFirstLetterUppercase( $input )
{
$result = $firstLetter . $otherLetters ;
return $result ;
}
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $input )
{
$lastSymbol = $input % 10 ;
if ( $lastSymbol == 1 ) {
$result = ' рубль.' ;
} elseif ( ( $lastSymbol >= 2 ) && ( $lastSymbol <= 4 ) ) {
$result = ' рубля.' ;
} else {
$result = ' рублей.' ;
}
return $result ;
}
/**
* Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
* если мы считаем число для мужского рода (один рубль),
* и 1 — для женского (одна тысяча)
*/
function smallNumberToText( $input , $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 => 'две' ,
) ;
/* Заменим значения в массиве spelling (для женского рода) */
if ( $isFemale == 1 ) {
$spelling = array_replace( $spelling , $femaleSpelling ) ;
}
$input = ltrim ( $input , 0 ) ; /* Удалим в начале все нули, если они есть */ $flipped = array_flip ( $spelling ) ; /* Поменяем ключи и значения ключей местами, чтобы использовать функцию array_search */
if ( ( $input >= 0 ) && ( $input <= 20 ) ) {
} elseif ( ( $input >= 21 ) && ( $input <= 99 ) ) {
if ( $input % 10 == 0 ) {
} else {
$singleDigits = mb_substr ( $input , 1 , 1 ) ; /* Выделяем из числа послению цифру (число 89, то $singleDigits = 9) */ $tens = $input - $singleDigits ; /* Вычитаем из начального числа значение переменной $singleDigits (число 89 - $singleDigits, то $tens = 80) */
$result = $tens . " " . $singleDigits ;
}
} elseif ( ( $input >= 100 ) && ( $input <= 999 ) ) {
if ( $input % 100 == 0 ) {
} else {
$tens = mb_substr ( $input , 1 , 2 ) ; /* Выделяем из числа посление две цифры (число 989, то $tens = 89) */ $hundreds = $input - $tens ; /* Вычитаем из начального числа значение переменной $tens (число 989 - $tens, то $hundreds = 900) */
$singleDigits = mb_substr ( $tens , 1 , 1 ) ; /* Выделяем последениею цифру из перемееной $tens (число 89, то $singleDigits = 9) */ if ( ( $tens % 10 == 0 ) || ( ( $tens >= 0 ) && ( $tens <= 20 ) ) ) {
$result = $hundreds . " " . $tens ;
} else {
$tens = $tens - $singleDigits ;
$result = $hundreds . " " . $tens . " " . $singleDigits ;
}
}
}
return $result ;
}
function numberToText( $input )
{
$thousandSpelling = array ( '1' => 'тысяча' ,
'2' => 'тысячи' ,
'3' => 'тысячь' ,
) ;
$millionSpelling = array ( '1' => 'миллион' ,
'2' => 'миллиона' ,
'3' => 'миллионов' ,
) ;
$lengthNum = mb_strlen ( $input ) ; /* Найдем длинну числа */ $result = '' ;
if ( ( $lengthNum <= 8 ) && ( $lengthNum >= 7 ) ) {
/* Разбиваем число */
if ( $lengthNum == 8 ) {
} elseif ( $lengthNum == 7 ) {
}
/* Преобразуем разбитые числа в текст */
$aText = smallNumberToText( $a , 0 ) ;
$bText = smallNumberToText( $b , 1 ) ;
$cText = smallNumberToText( $c , 0 ) ;
/* Преобразуем миллионы в текст */
$millions = '' ;
if ( ( $a >= 11 ) && ( $a <= 14 ) ) {
$millions .= $aText . " " . $millionSpelling [ 3 ] . " " . $bText ;
} else {
$lastDigits = $a % 10 ;
if ( $lastDigits == 1 ) {
$millions .= $aText . " " . $millionSpelling [ 1 ] . " " . $bText ;
} elseif ( ( $lastDigits >= 2 ) && ( $lastDigits <= 4 ) ) {
$millions .= $aText . " " . $millionSpelling [ 2 ] . " " . $bText ;
} else {
$millions .= $aText . " " . $millionSpelling [ 3 ] . " " . $bText ;
}
}
/* Преобразуем тысячи в текст */
$b = $b % 10 ;
if ( $b == 1 ) {
$thousand = $thousandSpelling [ 1 ] ;
} elseif ( ( $b > 1 ) && ( $b <= 4 ) ) {
$thousand = $thousandSpelling [ 2 ] ;
} else {
$thousand = $thousandSpelling [ 3 ] ;
}
/* Соединяем миллионы и тысячи */
$result = $millions . " " . $thousand . " " . $cText ;
} elseif ( ( $lengthNum <= 6 ) && ( $lengthNum >= 4 ) ) {
/* Разбиваем число */
if ( $lengthNum == 6 ) {
} elseif ( $lengthNum == 5 ) {
} elseif ( $lengthNum == 4 ) {
}
/* Преобразуем разбитые числа в текст */
$aText = smallNumberToText( $a , 1 ) ;
$bText = smallNumberToText( $b , 0 ) ;
/* Преобразуем тысячи в текст */
if ( $a % 10 == 1 ) {
$thousand = $thousandSpelling [ 1 ] ;
} elseif ( ( $a % 10 > 1 ) && ( $a % 10 <= 4 ) ) {
$thousand = $thousandSpelling [ 2 ] ;
} else {
$thousand = $thousandSpelling [ 3 ] ;
}
/* Соединяем тысячи и сотни */
$result = $aText . " " . $thousand . " " . $bText ;
} elseif ( $lengthNum <= 3 ) {
$result = smallNumberToText( $input , 0 ) ;
}
$result = makeFirstLetterUppercase( $result ) ;
$ending = inclineWord( $input ) ;
$result .= $ending ;
return $result ;
}
$amount = 11011011 ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = mt_rand ( 1000000 , 9999999 ) ; $text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = mt_rand ( 10000000 , 99999999 ) ; $text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
PD9waHAKCi8qKgogKiAwNS4wNS4yMDE1ICgyMDoxNykKICogVGhlTnVtYmVycy5waHAKICogUGhwU3Rvcm0KICovCgplcnJvcl9yZXBvcnRpbmcoLTEpOwptYl9pbnRlcm5hbF9lbmNvZGluZygndXRmLTgnKTsKaGVhZGVyKCJDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgiKTsKCi8qINCU0LXQu9Cw0LXRgiDQv9C10YDQstGD0Y4g0LHRg9C60LLRgyDQv9GA0LXQtNC70L7QttC10L3QuNGPINC30LDQs9C70LDQstC90L7QuSAqLwpmdW5jdGlvbiBtYWtlRmlyc3RMZXR0ZXJVcHBlcmNhc2UoJGlucHV0KQp7CiAgICAkZmlyc3RMZXR0ZXIgPSBtYl9zdWJzdHIoJGlucHV0LCAwLCAxKTsKICAgICRmaXJzdExldHRlciA9IG1iX3N0cnRvdXBwZXIoJGZpcnN0TGV0dGVyKTsKICAgICRvdGhlckxldHRlcnMgPSBtYl9zdWJzdHIoJGlucHV0LCAxKTsKICAgICRyZXN1bHQgPSAkZmlyc3RMZXR0ZXIgLiAkb3RoZXJMZXR0ZXJzOwogICAgcmV0dXJuICRyZXN1bHQ7Cn0KCi8qINCS0L7Qt9Cy0YDQsNGJ0LDQtdGCINGB0L7QvtGC0LLQtdGC0YHRgtCy0YPRjtGJ0YPRjiDRh9C40YHQu9GDINGE0L7RgNC80YMg0YHQu9C+0LLQsDogMSDRgNGD0LHQu9GMLCAyINGA0YPQsdC70Y8sIDUg0YDRg9Cx0LvQtdC5ICovCmZ1bmN0aW9uIGluY2xp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H0LjRgdC70L4gOTg5LCDRgtC+ICR0ZW5zID0gODkpICovCiAgICAgICAgICAgICRodW5kcmVkcyA9ICRpbnB1dCAtICR0ZW5zOyAvKiDQktGL0YfQuNGC0LDQtdC8INC40Lcg0L3QsNGH0LDQu9GM0L3QvtCz0L4g0YfQuNGB0LvQsCDQt9C90LDRh9C10L3QuNC1INC/0LXRgNC10LzQtdC90L3QvtC5ICR0ZW5zICjRh9C40YHQu9C+IDk4OSAtICR0ZW5zLCDRgtC+ICRodW5kcmVkcyA9IDkwMCkgKi8KICAgICAgICAgICAgJHNpbmdsZURpZ2l0cyA9IG1iX3N1YnN0cigkdGVucywgMSwgMSk7IC8qINCS0YvQtNC10LvRj9C10Lwg0L/QvtGB0LvQtdC00LXQvdC40LXRjiDRhtC40YTRgNGDINC40Lcg0L/QtdGA0LXQvNC10LXQvdC+0LkgJHRlbnMgKNGH0LjRgdC70L4gODksINGC0L4gJHNpbmdsZURpZ2l0cyA9IDkpICovCiAgICAgICAgICAgIGlmICgoJHRlbnMgJSAxMCA9PSAwKSB8fCAoKCR0ZW5zID49IDApICYmICgkdGVucyA8PSAyMCkpKSB7CiAgICAgICAgICAgICAgICAkaHVuZHJlZHMgPSBhcnJheV9zZWFyY2goJGh1bmRyZWRzLCAkZmxpcHBlZCk7CiAgICAgICAgICAgICAgICAkdGVucyA9IGFycmF5X3NlYXJjaCgkdGVucywgJGZsaXBwZWQpOwogICAgICAgICAgICAgICAgJHJlc3VsdCA9ICRodW5kcmVkcyAuICIgIiAuICR0ZW5zOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgJHRlbnMgPSAkdGVucyAtICRzaW5nbGVEaWdpdHM7CiAgICAgICAgICAgICAgICAkaHVuZHJlZHMgPSBhcnJheV9zZWFyY2goJGh1bmRyZWRzLCAkZmxpcHBlZCk7CiAgICAgICAgICAgICAgICAkdGVucyA9IGFycmF5X3NlYXJjaCgkdGVucywgJGZsaXBwZWQpOwogICAgICAgICAgICAgIC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AgICAgICB9IGVsc2VpZiAoJGxlbmd0aE51bSA9PSA3KSB7CiAgICAgICAgICAgICRhID0gbWJfc3Vic3RyKCRpbnB1dCwgMCwgMSk7CiAgICAgICAgICAgICRiID0gbWJfc3Vic3RyKCRpbnB1dCwgMSwgMyk7CiAgICAgICAgICAgICRjID0gbWJfc3Vic3RyKCRpbnB1dCwgNCwgMyk7CiAgICAgICAgfQogICAgICAgIC8qINCf0YDQtdC+0LHRgNCw0LfRg9C10Lwg0YDQsNC30LHQuNGC0YvQtSDRh9C40YHQu9CwINCyINGC0LXQutGB0YIgKi8KICAgICAgICAkYVRleHQgPSBzbWFsbE51bWJlclRvVGV4dCgkYSwgMCk7CiAgICAgICAgJGJUZXh0ID0gc21hbGxOdW1iZXJUb1RleHQoJGIsIDEpOwogICAgICAgICRjVGV4dCA9IHNtYWxsTnVtYmVyVG9UZXh0KCRjLCAwKTsKICAgICAgICAvKiDQn9GA0LXQvtCx0YDQsNC30YPQtdC8INC80LjQu9C70LjQvtC90Ysg0LIg0YLQtdC60YHRgiAqLwogICAgICAgICRtaWxsaW9ucyA9ICcnOwogICAgICAgIGlmICgoJGEgPj0gMTEpICYmICgkYSA8PSAxNCkpIHsKICAgICAgICAgICAgJG1pbGxpb25zIC49ICRhVGV4dCAuICIgIiAuICRtaWxsaW9uU3BlbGxpbmdbM10gLiAiICIgLiAkYlRleHQ7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgJGxhc3REaWdpdHMgPSAkYSAlIDEwOwogICAgICAgICAgICBpZiAoJGxhc3REaWdpdHMgPT0gMSkgewogICAgICAgICAgICAgICAgJG1pbGxpb25zIC49ICRhVGV4dCAuICIgIiAuICRtaWxsaW9uU3BlbGxpbmdbMV0gLiAiICIgLiAkYlRleHQ7CiAgICAgICAgICAgIH0gZWxzZWlmICgoJGxhc3REaWdpdHMgPj0gMikgJiYgKCRsYXN0RGlnaXRzIDw9IDQpKSB7CiAgICAgICAgICAgICAgICAkbWlsbGlvbnMgLj0gJGFUZXh0IC4gIiAiIC4gJG1pbGxpb25TcGVsbGluZ1syXSAuICIgIiAuICRiVGV4dDsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICRtaWxsaW9ucyAuPSAkYVRleHQgLiAiICIgLiAkbWlsbGlvblNwZWxsaW5nWzNdIC4gIiAiIC4gJGJUZXh0OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIC8qINCf0YDQtdC+0LHRgNCw0LfRg9C10Lwg0YLRi9GB0Y/Rh9C4INCyINGC0LXQutGB0YIgKi8KICAgICAgICAkYiA9ICRiICUgMTA7CiAgICAgICAgaWYgKCRiID09IDEpIHsKICAgICAgICAgICAgJHRob3VzYW5kID0gJHRob3VzYW5kU3BlbGxpbmdbMV07CiAgICAgICAgfSBlbHNlaWYgKCgkYiA+IDEpICYmICgkYiA8PSA0KSkgewogICAgICAgICAgICAkdGhvdXNhbmQgPSAkdGhvdXNhbmRTcGVsbGluZ1syXTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAkdGhvdXNhbmQgPSAkdGhvdXNhbmRTcGVsbGluZ1szXTsKICAgICAgICB9CiAgICAgICAgLyog0KHQvtC10LTQuNC90Y/QtdC8INC80LjQu9C70LjQvtC90Ysg0Lgg0YLRi9GB0Y/Rh9C4ICovCiAgICAgICAgJHJlc3VsdCA9ICRtaWxsaW9ucyAuICIgIiAuICR0aG91c2FuZCAuICIgIiAuICRjVGV4dDsKICAgIH0gZWxzZWlmICgoJGxlbmd0aE51bSA8PSA2KSAmJiAoJGxlbmd0aE51bSA+PSA0KSkgewogICAgICAgIC8qINCg0LDQt9Cx0LjQstCw0LXQvCDRh9C40YHQu9C+ICovCiAgICAgICAgaWYgKCRsZW5ndGhOdW0gPT0gNikgewogICAgICAgICAgICAkYSA9IG1iX3N1YnN0cigkaW5wdXQsIDAsIDMpOwogICAgICAgICAgICAkYiA9IG1iX3N1YnN0cigkaW5wdXQsIDMsIDMpOwogICAgICAgIH0gZWxz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