<?php

/**
 * 05.05.2015 (20:17)
 * TheNumbers.php
 * PhpStorm
 */

error_reporting(-1);
mb_internal_encoding('utf-8');
header("Content-Type: text/plain; charset=utf-8");

/* Делает первую букву предложения заглавной */
function makeFirstLetterUppercase($input)
{
    $firstLetter = mb_substr($input, 0, 1);
    $firstLetter = mb_strtoupper($firstLetter);
    $otherLetters = mb_substr($input, 1);
    $result = $firstLetter . $otherLetters;
    return $result;
}

/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord($input, $word1, $word2, $word5)
{
    if (($input >= 11) && ($input <= 14)) {
        $result = $word5;
    } else {
        $lastSymbol = $input % 10;
        if ($lastSymbol == 1) {
            $result = $word1;
        } elseif (($lastSymbol >= 2) && ($lastSymbol <= 4)) {
            $result = $word2;
        } else {
            $result = $word5;
        }
    }
    return $result;
}

/**
 * Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
 * если мы считаем число для мужского рода (один рубль),
 * и 1 — для женского (одна тысяча)
 */
function smallNumberToText($input, $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 => 'две',
    );

    $text = array();

    if ($isFemale == 1) {
        $spelling = array_replace($spelling, $femaleSpelling);
    }

    $singleDigits = $input % 10;
    $tens = $input % 100;
    $tensSave = $tens - $singleDigits;
    $hundreds = $input - $tens;

    if ($input == 0) {
        $text[] = $spelling[$input];
    }
    if ($hundreds != 0) {
        $text[] = $spelling[$hundreds];
    }
    if (($tens >= 1) && ($tens <= 19)) {
        $text[] = $spelling[$tens];
    }
    if (($tens >= 20) && ($tens <= 99)) {
        $text[] = $spelling[$tensSave];
        if ($singleDigits != 0) {
            $text[] = $spelling[$singleDigits];
        }
    }

    $result = implode(" ", $text);

    return $result;
}

function numberToText($input)
{
    $text = array();

    $million = floor($input / 1000000);
    $thousand = floor(($input - ($million * 1000000)) / 1000);
    $hundreds = $input % 1000;

    if ($million != 0) {
        $text[] = smallNumberToText($million, 0);
        $text[] = inclineWord($million, 'миллион', 'миллиона', 'миллионов');
    }
    if ($thousand != 0) {
        $text[] = smallNumberToText($thousand, 1);
        $text[] = inclineWord($thousand, 'тысяча', 'тысячи', 'тысяч');
    }
    if ($hundreds != 0) {
        $text[] = smallNumberToText($hundreds, 0);
        $text[] = inclineWord($hundreds, 'рубль.', 'рубля.', 'рублей.');
    }

    $result = implode(" ", $text);
    $result = makeFirstLetterUppercase($result);

    return $result;
}

$amount = mt_rand(0, 999);
$text = numberToText($amount);
echo "На вашем счету: {$text} ({$amount})\n";

$amount = mt_rand(1000, 9999);
$text = numberToText($amount);
echo "На вашем счету: {$text} ({$amount})\n";

$amount = mt_rand(10000, 99999);
$text = numberToText($amount);
echo "На вашем счету: {$text} ({$amount})\n";

$amount = mt_rand(100000, 999999);
$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";

$amount = 11011011;
$text = numberToText($amount);
echo "На вашем счету: {$text} ({$amount})\n";

$amount = 11012013;
$text = numberToText($amount);
echo "На вашем счету: {$text} ({$amount})\n";