<?php

// Staring straight up into the sky ... oh my my
error_reporting(-1);
mb_internal_encoding('utf-8');


/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord($number, $word1, $word2, $word5) {
	if ($number % 100 == 11 || $number % 100 == 12 || $number % 100 == 13 || $number % 100 == 14) return $word5;
    if ($number % 10 == 1) return $word1;
    if ($number % 10 == 2 || $number % 10 == 3 || $number % 10 == 4) return $word2;
    return $word5;
}

/* 
    Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю, 
    если мы считаем число для мужского рода (один рубль), 
    и 1 — для женского (одна тысяча) 
*/
function smallNumberToText($number, $isFemale) {

    $spelling = array(
        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   =>  'две'
    );

    if ($isFemale) {
        $spelling[1] = $femaleSpelling[1];
        $spelling[2] = $femaleSpelling[2];
    }

    if ($number < 20) {
        return $spelling[$number];
    } elseif ($number < 100 && $number % 10 != 0) {
        $units = $number % 10;
        $dozens = $number - $units;
        return $spelling[$dozens]." ".$spelling[$units];
    } elseif ($number < 100 && $number % 10 == 0) return $spelling[$number];

    $hundreds = $number - $number % 100;
    
    if ($number % 100 < 20 && $number % 100 != 0) {
        $dozens = $number % 100;
        return $spelling[$hundreds]." ".$spelling[$dozens];
    } elseif ($number % 100 == 0) {
        return $spelling[$hundreds];
    } elseif ($number % 10 == 0) {
        $dozens = $number % 100;
        $hundreds = $number - $dozens;
        return $spelling[$hundreds]." ".$spelling[$dozens];
    }

    $dozens = $number % 100 - $number % 10;
    $units = $number % 10;

    return $spelling[$hundreds]." ".$spelling[$dozens]." ".$spelling[$units];
}


function numberToText($number) {

    if ($number < 1000) {
        return smallNumberToText($number, 0)." ".inclineWord($number, "рубль", "рубля", "рублей");
    } elseif ($number < 1000000) {
        $thousands = ($number - $number % 1000) / 1000;
        $hundreds = $number % 1000;

        if ($hundreds == 0) {
            return smallNumberToText($thousands, 1)." ".inclineWord($thousands, "тысяча", "тысячи", "тысяч")." ".inclineWord($number, "рубль", "рубля", "рублей");
        }

        return smallNumberToText($thousands, 1)." ".inclineWord($thousands, "тысяча", "тысячи", "тысяч")." ".smallNumberToText($hundreds, 0)." ".inclineWord($number, "рубль", "рубля", "рублей");
    } elseif ($number < 1000000000) {
        $millions = floor($number / 1000000);
        $thousands = floor(($number % 1000000) / 1000);
        $hundreds = $number % 1000;

        if ($thousands == 0 && $hundreds == 0) {
            return smallNumberToText($millions, 0)." ".inclineWord($millions, "миллион", "миллиона", "миллионов")." ".inclineWord($number, "рубль", "рубля", "рублей"); 
        } elseif ($thousands == 0) {
            return smallNumberToText($millions, 0)." ".inclineWord($millions, "миллион", "миллиона", "миллионов")." ".smallNumberToText($hundreds, 0)." ".inclineWord($number, "рубль", "рубля", "рублей");
        } elseif ($hundreds == 0) {
            return smallNumberToText($millions, 0)." ".inclineWord($millions, "миллион", "миллиона", "миллионов")." ".smallNumberToText($thousands, 1)." ".inclineWord($thousands, "тысяча", "тысячи", "тысяч")." ".inclineWord($number, "рубль", "рубля", "рублей");
        }

        return smallNumberToText($millions, 0)." ".inclineWord($millions, "миллион", "миллиона", "миллионов")." ".smallNumberToText($thousands, 1)." ".inclineWord($thousands, "тысяча", "тысячи", "тысяч")." ".smallNumberToText($hundreds, 0)." ".inclineWord($number, "рубль", "рубля", "рублей");
    }
}

/* Вызовем функцию несколько раз */
$amount1 = mt_rand(1,99999999);
$text1 = numberToText($amount1);

echo "На вашем счету {$text1}\n";

$amount2 = mt_rand(1,99999999);
$text2 = numberToText($amount2);

echo "На вашем счету {$text2}\n";

$amount3 = 11012013;
$text3 = numberToText($amount3);

echo "На вашем счету {$text3}\n";

$amount4 = 1000001;
$text4 = numberToText($amount4);

echo "На вашем счету {$text4}\n";

$amount7 = 100000;
$text7 = numberToText($amount7);

echo "На вашем счету {$text7}\n";

