<?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) {
    /* DIY */
    if ($number == 1) {
        return "рубль";
    }elseif ($number == 2 || $number == 3 || $number == 4){
        return "рубля";
    }elseif ($number == 0 || $number >5 and $number<21){
        return "рублей";
    }else {
        return "рублей";
    }
}

/*
    Преобразует числа от 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 => 'две'
    );
    /* DIY */
    $splitter = preg_split('//u', $number, -1, PREG_SPLIT_NO_EMPTY);

    if (count($splitter) <3 and $number <= 20) {
        return $spelling[$number];
    } elseif ($number > 20 and $number < 100) {
        $decimal = $splitter[0] * 10;
        return ($spelling[$decimal] . " " . $spelling[$splitter[1]]);

    } elseif ($number >= 100 and $number < 1000) {
        // Тройка чисел!
        if (count($splitter) ==3 and $splitter[2]==0){
            $hundredth = $splitter[0] * 100;
            $decimal = $splitter[1] * 10;
            return ($spelling[$hundredth] . " " . $spelling[$decimal]);
        }elseif(count($splitter) ==3 and $splitter[1]==0){
            $hundredth = $splitter[0] * 100;
            return ($spelling[$hundredth] . " " . $spelling[$splitter[2]]);
        } elseif(count($splitter) ==3 and $splitter[0]==0 and $splitter[1]==0){
            $hundredth = $splitter[0] * 100;
            return ($spelling[$hundredth]);
        } elseif(count($splitter) ==3 and $splitter[0]==0){
            $decimal = $splitter[1] * 10;
            return ($spelling[$decimal] . " " . $spelling[$splitter[2]]);
        } elseif(count($splitter) ==3 and  $splitter[0]==0 and $splitter[1]==0){
            return ($spelling[$splitter[2]]);
        }else{
            $hundredth = $splitter[0] * 100;
            $decimal = $splitter[1] * 10;
            return ($spelling[$hundredth] . " " . $spelling[$decimal] . " " . $spelling[$splitter[2]]);
        }
    } elseif ($number > 1000 and $number < 2000) {
        echo "Принимаю больше тысячи - " . $number;
        $numbers = preg_split('//u', $number, -1, PREG_SPLIT_NO_EMPTY);
        if (($numbers[2]) == 0) {
            $thousands = $numbers[0] * 1000;
            $hundredth = $numbers[0] * 100;
            return ($spelling[$thousands] . " " . $spelling[$hundredth] . " " . $spelling[$numbers[2]]);

        } elseif (($numbers[1]) == 0 and ($numbers[2]) == 0) {
            $thousands = $numbers[0] * 1000;
            return ($spelling[$thousands] . " " . $spelling[$numbers[2]]);

        } elseif (($numbers[1]) == 0) {
            $thousands = $numbers[0] * 1000;
            $decimal = $numbers[2] * 10;
            return ($spelling[$thousands] . " " . ($spelling[$decimal]) . " " . $spelling[$numbers[2]]);
        } else {
            $thousands = $numbers[0] * 1000;
            $hundredth = $numbers[1] * 100;
            $decimal = $numbers[2] * 10;
            return ($femaleSpelling[1] . " " . $thousands[1] . " " . $spelling[$hundredth] . " " . $spelling[$decimal] . " " . $spelling[$numbers[3]]);
        }
    }

    else {
    }
}

function numberToText($number) {

    /* DIY */
    $resultString="";
    $lastTreeDigit="";
    echo "Исходное число ".$number."\n";

    $separatedNumbers= preg_split('//u', $number, NULL , PREG_SPLIT_NO_EMPTY);
    $j=count($separatedNumbers);
    $lastDigit=array_pop($separatedNumbers);

    // один
    $spedec = array(
        0   =>  'Один',
        1   =>  'десять',
        2   =>  'сто',
        3   =>  'тысяча',
        4   =>  'миллион',
        5   =>  'миллиард',
        6   =>  'триллион',
        7   =>  'квадриллион',
        8   =>  'квинтиллион',
        9   =>  'секстиллион',
        10  =>  'септиллион',
        11  =>  'октиллион',
        12  =>  'нониллион',
        13  =>  'дециллион'
    );

    //пять
    $spedecSecond = array(
        1   =>  'десять',
        2   =>  'сто',
        3   =>  'тысячь',
        4   =>  'миллионов',
        5   =>  'миллиардов',
        6   =>  'триллионов',
        7   =>  'квадриллионов',
        8   =>  'квинтиллионов',
        9   =>  'секстиллионов',
        10  =>  'септиллионов',
        11  =>  'октиллионов',
        12  =>  'нониллионов',
        13  =>  'дециллионов'
    );

    // три
    $spedecThird = array(
        1   =>  'десять',
        2   =>  'сто',
        3   =>  'тысячи',
        4   =>  'миллиона',
        5   =>  'миллиарда',
        6   =>  'триллиона',
        7   =>  'квадриллиона',
        8   =>  'квинтиллиона',
        9   =>  'секстиллиона',
        10  =>  'септиллиона',
        11  =>  'октиллиона',
        12  =>  'нониллиона',
        13  =>  'дециллиона'
    );

    for ($i=0; $i <count($separatedNumbers); $i++) {
        $j--;
        if ($j>2){
            switch  (($separatedNumbers[$i])) {
                case 1;
                    $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedec[$j];
                    break;
                case 2;
                case 3;
                case 4;
                    $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedecThird[$j];
                    break;
                case 5;
                case 6;
                case 7;
                case 8;
                case 9;
                case 10:
                    $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedecSecond[$j];
                    break;
                default;
                    $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedec[$j];
                    break;
            }
        }else{
            $lastTreeDigit = $lastTreeDigit .  $separatedNumbers[$i];
        }
    }
    $resultString = $resultString . " ". smallNumberToText(($lastTreeDigit. $lastDigit), 0). " ". inclineWord($lastDigit, 1, 1, 1) . " ";;
    $result=$resultString;
    return $result;
}

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

$amount5 = mt_rand(1,9);
$text5 = numberToText($amount5);
echo "На вашем счету {$text5} \n";

$amount6 = 9009;
$text6 = numberToText($amount6);
echo "На вашем счету {$text6} \n";

$amount7 = 999999999;
$text7 = numberToText($amount7);
echo "На вашем счету {$text7} \n";