<?php
 
error_reporting(-1);
mb_internal_encoding('utf-8');
 
 
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord($key, $word) {
 
    $end = array(
	    0 => array(
		    0 => "миллион",
			1 => "миллиона",
			2 => "миллионов"
		),
		1 => array(
		    0 => "тысяча",
			1 => "тысячи",
			2 => "тысяч"
		),
		2 => array(
		    0 => "рубль",
			1 => "рубля",
			2 => "рублей"
		)
    );
 
    if ($word == 1){
        $word = $end[$key][0];
    } elseif (($word >= 2) && ($word <= 4)) {
	    $word = $end[$key][1];
    } else {
	    $word = $end[$key][2];
    }
 
	return($word);
}
 
//разбивает число на миллионы, тысяча и сотни
function parts($number) {
	$score = mb_strlen($number);
	
	$hundred = $number % 1000;
   
	if ($score >= 6) {
        $thousand = mb_substr($number, -6, 3);
    } elseif ($score == 5) {
	    $thousand = mb_substr($number, -5, 2);
    } elseif ($score == 4) {
	    $thousand = mb_substr($number, -4, 1);
    }
 
    if ($score == 9) {
	    $million = mb_substr($number, 0, 3);
    } elseif ($score == 8) {
	    $million = mb_substr($number, 0, 2);
    } elseif ($score == 7) {
		$million = mb_substr($number, 0, 1);
	}
 
	$level = array(
	    0 => $million,
		1 => $thousand,
		2 => $hundred
	);
	
	return($level);
}
 
//переводит числовое число в текстовое 
function smallNumberToText($number) {

	
	if ($number == 0) {
		echo "На вашем счету ноль рублей\n";
		exit;
	}
	
    $spelling = array(
        0   =>  'ноль',                                           
        1   =>  'один',                   
        2   =>  'два',                   
        3   =>  'три',                        
        4   =>  'четыре',              
        5   =>  'пять',                
        6   =>  'шесть',                   
        7   =>  'семь',                 
        8   =>  'восемь',            
        9   =>  'девять',
        10  =>  'десять', 	
        11  =>  'одиннадцать',	
        12  =>  'двенадцать',  
        13  =>  'тринадцать', 
        14  =>  'четырнадцать',  
        15  =>  'пятнадцать',   
        16  =>  'шестнадцать', 
        17  =>  'семнадцать',  
        18  =>  'восемнадцать',    
	    19  =>  'девятнадцать',  
        20  =>  'двадцать', 	
        30  =>  'тридцать',
        40  =>  'сорок',
        50  =>  'пятьдесят',
        60  =>  'шестьдесят',
        70  =>  'семьдесят', 
        80  =>  'восемьдесят',
        90  =>  'девяносто',
        100 =>  'сто',
        200 =>  'двести',
        300 =>  'триста',
        400 =>  'четыреста',
        500 =>  'пятьсот',
        600 =>  'шестьсот',
        700 =>  'семьсот', 
        800 =>  'восемьсот',
        900 =>  'девятьсот',
    ); 
	
	$level = parts($number);
	
	$text = "";
 
	foreach($level as $key => $discharge) {
 
        if ($discharge > 0) {
 
		    if (mb_strlen($discharge) == 3) {
				$text = $text.' '.$spelling[mb_substr($discharge, 0, 1) * 100];
				$discharge = mb_substr($discharge, -2);
			}
 
			if (($discharge % 100 >= 10) && ($discharge % 100 <= 19)) {
 
				$text = $text.' '.$spelling[$discharge % 100];
				$word = $discharge;
				$words = inclineWord($key, $word);
				$text = $text.' '.$words; 	
 
			} else {
 				
				if (mb_strlen($discharge) == 2) {
	                $numberDigits = 100;
	            }   elseif (mb_strlen($discharge) == 1) {
	                $numberDigits = 10;
	            }
 
				for ($i = 0; $i < mb_strlen($discharge); $i++) {
			        $numberDigits = $numberDigits / 10;
			        $text = $text.' '.$spelling[mb_substr($discharge, $i, 1) * $numberDigits];
	            }
				
				$word = $discharge % 10;
				$words = inclineWord($key, $word);
				$text = $text.' '.$words;
			}
		}
    }
    
	$regexp = array('/один тысяча/', '/два тысячи/', '/ ноль/', '/^[ ]/');
	$replacements = array('одна тысяча', 'две тысячи', '', '');
	$text = preg_replace($regexp, $replacements, $text);
	
	if ($number % 1000 == 000) {
		$text = $text.' '.'рублей';
	}
	 
	return($text);
}
 
$number = mt_rand(0, 999999999);
$number = smallNumberToText($number);
echo "На вашем счету {$number}\n";