<?php

error_reporting(-1);

/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord($number, $word1, $word2, $word3) {

    if ($number % 100 >= 11 and $number % 100 <= 19) {
        return $word3;
    }

    if ($number % 10 == 1) {
        $word = $word1;
    } elseif ($number % 10 >= 2 and $number % 10 <= 4) {
        $word = $word2;
    } else {
        $word = $word3;
    }

    return $word;
}

/* 
    Преобразует числа от 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  =>  'девятнадцать'    
    );

    if ($isFemale) {
        if ($number % 10 == 1 or $number % 10 == 2) {
            $femaleSpelling = array(
                1   =>  'одна',  
                2   =>  'две'
            );
            $spelling = array_replace($spelling, $femaleSpelling);
        }
    }

    $words = array();
    
    if ($number == 0) {
        $words[] = $spelling[$number];
    } elseif ($number >= 100) {
        $words[] = $spelling[floor($number / 100) * 100];
    }

    if ($number % 100 == 10 or $number % 100 >= 20 and $number % 100 <= 99) {
        $words[] = $spelling[floor(($number % 100) / 10) * 10];
    }

    if ($number % 100 >= 11 and $number % 100 <= 19) {
        $words[] = $spelling[$number % 100];
    } elseif ($number % 10 != 0) {
        $words[] = $spelling[$number % 10];
    }

    return implode(' ', $words);
}

function numberToText($number) {

    $divisibleNumber = $number;

    $spelling = array(
        0 => array(0 => 'рубль', 1 => 'рубля', 2 => 'рублей'),
        1 => array(0 => 'тысяча', 1 => 'тысячи', 2 => 'тысяч'),
        2 => array(0 => 'миллион', 1 => 'миллиона', 2 => 'миллионов')
    );
    
    $lengthOfNumber = floor(log10($number) + 1);

    for ($i = 0; $i < ceil($lengthOfNumber / 3) or $i == 0; $i++) {

        $numbers[] = $divisibleNumber % 1000;
        $divisibleNumber = floor($divisibleNumber / 1000);

        $isFemale = ($i == 1) ? 1 : null;

        $numberToText = smallNumberToText($numbers[$i], $isFemale);
        // Обработка нуля
        if ($numberToText == 'ноль' and $lengthOfNumber > 0) {
            $numberToText = null;
        }
        $inclinedWord = inclineWord($numbers[$i], $spelling[$i][0], $spelling[$i][1], $spelling[$i][2]);

        if ($i == 0) {
            $text = "($number)".' '.$inclinedWord;
            if ($numberToText) {
                $text = $numberToText.' '.$text;
            }
        } elseif ($numberToText) {
            $text = $numberToText.' '.$inclinedWord.' '.$text;
        }
    }

    return $text;
}

/* Вызовем функцию несколько раз */
$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";