<?php
 
// Staring straight up into the sky ... oh my my
error_reporting(-1);
 
 
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord($number, $word1, $word2, $word5) {

   if ($number % 100 >= 11 and $number % 100 <= 19) {
        return $word5;
    }
 
    if ($number % 10 == 1) {
        return $word1;
    } elseif ($number % 10 >= 2 and $number % 10 <= 4) {
        return $word2;
    } else {
        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 == true) {
        $spelling = array_replace($spelling, $femaleSpelling);
    }

    $words = array();
        
    if ($number >= 100) {
            $hundreds = floor($number / 100) * 100;
            $decades = $number % 100;
            $numbers = $decades % 10;

            if (array_key_exists($number, $spelling)) {
                array_push($words, $spelling[$number]);
            } elseif (array_key_exists($decades, $spelling)) {
                array_push($words, $spelling[$hundreds], $spelling[$decades]);
            } else {
                $decades = $decades - $numbers;
                array_push($words, $spelling[$hundreds], $spelling[$decades], $spelling[$numbers]);
            }
    } elseif ($number >= 10) {
        $decades = floor($number / 10) * 10;
        $numbers = $number % 10;
        if (array_key_exists($number, $spelling)) {
            array_push($words, $spelling[$number]);
        } else {
            array_push($words, $spelling[$decades], $spelling[$numbers]);
        }
    } elseif ($number >= 1) {
        array_push($words, $spelling[$number]);
    }    

    $output = implode(' ', $words);
    return $output;
}
 
function numberToText($number) {
  
    $constructing = array();
    $numbers = $number;
    $thousandsForThousands = intval(floor($number / 1000));

    if ($number > 999999) {
        $millions = intval(floor($number / 1000000));
        $thousandsForMillions = intval(floor($number % 1000000 / 1000));
        $numbersForMillions = $number - ($millions * 1000000) - ($thousandsForMillions * 1000); 
        $numbers = $numbersForMillions;
        $thousandsForThousands = $thousandsForMillions;
        $addMilions = smallNumberToText($millions, null).' '.inclineWord($millions, 'миллион', 'миллиона', 'миллионов');
        array_push($constructing, $addMilions);
    }

    if ($number > 999) {
        $thousands = intval(floor($number / 1000));
        $numbersForThousands = $number - ($thousands * 1000);
        if ($number < 1000000) {
            $numbers = $numbersForThousands;
        }
        $addThousands = smallNumberToText($thousandsForThousands, 1).' '.inclineWord($thousandsForThousands, 'тысяча', 'тысячи', 'тысяч');

        if ($thousandsForThousands != 0) {
            array_push($constructing, $addThousands);
        }
    }

    if ($number > 0) {
        $addNumbers = smallNumberToText($numbers, null).' '.inclineWord($numbers, 'рубль', 'рубля', 'рублей')." ({$number})";
        array_push($constructing, $addNumbers);
    }

    $total = implode(' ', $constructing);
    return $total;
}
 
/* Вызовем функцию несколько раз */
$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";