<?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) {
	$last2Digits = $number % 100;
	$lastDigit = $number % 10;
    
    if ($last2Digits >= 10 && $last2Digits <= 19 || $lastDigit == 0 ) {
    	return $word5;
    } elseif ($lastDigit >= 2 && $lastDigit <= 4) {
    	return $word2;
    } elseif ($lastDigit == 1) {
    	return $word1;
    } else {
    	return $word5;
    }
    
    return $incline;
}
/* 
    Преобразует числа от 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   =>  'две'
    );
 
    $hundreds = $number % 1000;
    $tens = $number % 100;
    $digits = $number % 10;
    
    $hundredsRound = floor ($hundreds / 100)*100;
    $tensRound = floor ($tens / 10)*10;
    
    $spelling['null'] = "";
    
    if ($isFemale == 1) {
    	$spelling[1] = 'одна';
    	$spelling[2] = 'две';
    }
    
    if ($digits == 0 && ($tensRound > 10 || $hundredsRound > 100)) {
    	$digits = 'null';
    }
    
    if ($hundredsRound == 0) {
    	$hundredsRound = 'null';
    }
    
    if ($tensRound < 10) {
    	$tensRound = 'null';
    }
    
    if ($tens >= 10 && $tens <= 19) {
    	$digits = 'null';
    	$tensRound = $tens;
    }
    
    $numbersResult = $spelling[$hundredsRound].' '.$spelling[$tensRound].' '.$spelling[$digits];
    
    return $numbersResult;
}

function numberToText($number) {
	$millions = floor ($number / 1000000);
	$numberOfMillions = $millions % 1000;
	
	$thousands = floor ($number / 1000);
	$numberOfThousands = $thousands % 1000;
	
	$numberOfHundreds = $number % 1000;
	
	$wordsMillions = smallNumberToText($numberOfMillions, 0);
	$wordsThousands = smallNumberToText($numberOfThousands, 1);
	$wordsHundreds = smallNumberToText($numberOfHundreds, 0);
	
	$inclineWordsMillions = inclineWord($numberOfMillions, 'миллион', 'миллиона', 'миллионов');
	$inclineWordsThousands = inclineWord($numberOfThousands, 'тысяча', 'тысячи', 'тысяч');
	$inclineWordsHundreds = inclineWord($numberOfHundreds, 'рубль', 'рубля', 'рублей');
	
	if ($numberOfThousands == 0) {
		$wordsThousands = " ";
		$inclineWordsThousands = " ";
	}
	
	if ($numberOfHundreds == 0 && $number > 0) {
		$wordsHundreds = " ";
	}
	
	if ($number < 1000) {
		$totalResult = $wordsHundreds.' '.$inclineWordsHundreds;
	} elseif ($number < 1000000) {
		$totalResult = $wordsThousands.' '.$inclineWordsThousands.' '.$wordsHundreds.' '.$inclineWordsHundreds;
	} else {
	$totalResult = $wordsMillions.' '.$inclineWordsMillions.' '.$wordsThousands.' '.$inclineWordsThousands.' '.$wordsHundreds.' '.$inclineWordsHundreds;
	}
	
	$totalResult = preg_replace('/\s{2,}/', ' ', $totalResult);
	
	return $totalResult;
}
 
/* Вызовем функцию несколько раз */
$amount1 = mt_rand(1,99999999);
$text1 = numberToText($amount1);
 
echo "На вашем счету {$text1} ($amount1)\n";
 
$amount2 = mt_rand(1,99999999);
$text2 = numberToText($amount2);
 
echo "На вашем счету {$text2} ($amount2)\n";
 
$amount3 = mt_rand(1,99999999);
$text3 = numberToText($amount3);
 
echo "На вашем счету {$text3} ($amount3)\n";
 
$amount4 = mt_rand(1,99999999);
$text4 = numberToText($amount4);
 
echo "На вашем счету {$text4} ($amount4)\n";