<?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;
	if ($last2Digits > 19) { $last2Digits = $last2Digits % 10; }
	switch ($last2Digits) {
    	case 1:  { return " {$word1}"; }
    	case 2: case 3: case 4:  { return " {$word2}"; }
    	default: { return " {$word5}"; }
	}
}

/*
Преобразует числа от 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 => 'две'
    );
    
    $str = array();
    
    if($number == 0){
    	return null;
    }
    
    if($isFemale == 1){
    	if(array_key_exists($number, $femaleSpelling)){
    		return $femaleSpelling[$number];
    	}
    }
    else{
    	if (array_key_exists($number, $spelling)) {
    		return $spelling[$number];
    	}		
    }
    
    $last2Digits = $number % 100;
    
    $hundreds    = floor($number / 100) * 100;
    $tens        = floor($number % 100 / 10) * 10;
    $units       = $number % 10;
   
    if ($hundreds > 0) {
      	array_push($str, $spelling[$hundreds]);
    }
    if($last2Digits >= 11 && $last2Digits <= 19){
    	array_push($str, $spelling[$last2Digits]);
    }
    else{
    	if ($tens > 0) {
      		array_push($str, $spelling[$tens]);
    	}
    	if ($units > 0) {
    		if($isFemale == 1){
    			array_push($str, $femaleSpelling[$units]);			
    		}
    		else{
    			array_push($str, $spelling[$units]);		
    		}
    	}
    }	
    $str = implode($str, " ");
    return trim($str);
}

function numberToText($number)
{
	
	if($number == 0){
		$str = "ноль рублей";
		return $str;
	}
	
	$hundredsDigits = $number % 1000;
    $thousandsDigits = (floor($number / 1000)) % 1000;
    
    $checkFemale = $thousandsDigits % 10;
    
    if($checkFemale == 1 || $checkFemale == 2 || $thousandsDigits == 1 || $thousandsDigits == 2){
    	$thousandsWords = smallNumberToText($thousandsDigits, 1);
    }
    else{
    	$thousandsWords = smallNumberToText($thousandsDigits, 0);
    }
    
    $millionsDigits = ((floor($number / 1000)) / 1000) % 1000;
    
    $hundredsWords = smallNumberToText($hundredsDigits, 0);
    
    $millionsWords = smallNumberToText($millionsDigits, 0);
    
    if($millionsDigits > 0){
    	$millionsWords .= inclineWord($millionsDigits, "миллион", "миллиона", "миллионов");
    }
    if($thousandsDigits > 0){
    	$thousandsWords .= inclineWord($thousandsDigits, "тысяча", "тысячи", "тысяч");
    }
    
    $words = array($millionsWords, $thousandsWords, $hundredsWords);
    $words = array_filter($words);
   
    $str = implode($words, " ");
    $str .= inclineWord($number, "рубль", "рубля", "рублей");
    return trim($str);
}

/* Вызовем функцию несколько раз */
$amount1 = 901999999;
$text1   = numberToText($amount1);

echo "На вашем счету {$text1}\n";

$amount2 = 0;
$text2   = numberToText($amount2);

echo "На вашем счету {$text2}\n";

$amount3 = 1;
$text3   = numberToText($amount3);

echo "На вашем счету {$text3}\n";

$amount4 = 11012013;
$text4   = numberToText($amount4);

echo "На вашем счету {$text4}\n";

$amount5 = 7000008;
$text5   = numberToText($amount5);

echo "На вашем счету {$text5}\n";

$amount6 = 1000;
$text6   = numberToText($amount6);

echo "На вашем счету {$text6}\n";

$amount7 = 71000;
$text7   = numberToText($amount7);

echo "На вашем счету {$text7}\n";

$amount8 = 101000;
$text8   = numberToText($amount8);

echo "На вашем счету {$text8}\n";

$amount9 = 82000;
$text9   = numberToText($amount9);

echo "На вашем счету {$text9}\n";