<?php
 
// Staring straight up into the sky ... oh my my
error_reporting(-1);
 
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord($number, $word) {
	 if ($word == "миллион") {
		if ($number % 100 != 11 and $number % 10 == 1) {
			$word = "миллион";
		} elseif ($number % 100 != 12 and $number % 100 != 13 and $number % 100 != 14 and $number % 10 >= 2 and $number % 10 <= 4) {
			$word = "миллиона";
		} elseif ($number % 10 >= 5 and $number % 10 <= 9 or $number % 10 == 0 or $number % 100 >= 11 and $number % 100 <= 19) {
			$word = "миллионов";
		}
	} elseif ($word == "тысяча") {
		if ($number % 100 != 11 and $number % 10 == 1) {
			$word = "тысяча";
		} elseif ($number % 100 != 12 and $number % 100 != 13 and $number % 100 != 14 and $number % 10 >= 2 and $number % 10 <= 4) {
			$word = "тысячи";
		} elseif ($number % 10 >= 5 and $number % 10 <= 9 or $number % 10 == 0 or $number % 100 >= 11 and $number % 100 <= 19) {
			$word = "тысяч";
		}
	} elseif ($word == "рубль") {
		if ($number % 10 == 1) {
			$word = "рубль";
		} elseif ($number % 100 != 12 and $number % 100 != 13 and $number % 100 != 14 and $number % 10 >= 2 and $number % 10 <= 4) {
			$word = "рубля";
		} elseif ($number % 10 >= 5 and $number % 10 <= 9 or $number % 10 == 0 or $number % 100 >= 11 and $number % 100 <= 19) {
			$word = "рублей";
		}
	}
	return $word;
}
 
/* 
    Преобразует числа от 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 = floor($number / 100) * 100;                 
    $twoLastNumber = ($number % 100);                       
	if ($twoLastNumber >= 11 and $twoLastNumber <= 19) {    
		    $array = [];
            $array [] = "$hundreds";
            $array [] = "$twoLastNumber";
            $array1 = [];
           foreach($array as $a) {
               array_push($array1,$spelling[$a]);
		   }
	} else {
		    $tens = floor($twoLastNumber / 10) * 10;
            $units = $twoLastNumber - $tens;
			$array = [];
            $array [] = "$hundreds";
            $array [] = "$tens";
            $array [] = "$units";
            $array1 = [];
            foreach($array as $a) {	
                array_push($array1, $spelling[$a]);	
}
}
   if ($isFemale == 1) {
	   $array1 = preg_replace('/один/u', 'одна', $array1);
	   $array1 = preg_replace('/два/u', 'две', $array1);
   }
   $result = implode(" ", $array1);                         
   $result = trim($result);                                 
   return $result;
}
 
function numberToText($number) {
  if ($number > 0 and $number <= 999) {
	$thirst = $number % 1000;
} elseif ($number >= 1000 and $number <= 999999) {
	$second = floor($number / 1000);
    $thirst = $number % 1000;
} elseif ($number >= 1000000 - 999999999) {
	$first = floor($number / 1000000);
    $thirst = $number % 1000;
    $second = floor($number % 1000000 - $thirst) / 1000;
}
  if ($second % 10 == 1 or $second % 10 == 2) {
	  $isFemale = 1;
  } else {
	  $isFemale = 0;
  }
  
  $word1 = "миллион";
  $word2 = "тысяча";
  $word3 = "рубль";

  $million = inclineWord($first, $word1);
  $thousands = inclineWord($second, $word2);
  $rouble = inclineWord($thirst, $word3);

  $millionNumber = smallNumberToText($first, $isFemale);
  $thousandsNumber = smallNumberToText($second, $isFemale);
  $handredsNumber = smallNumberToText($thirst, $isFemale);

  $result = "$millionNumber $million, $thousandsNumber $thousands, $handredsNumber ($number) $rouble\n";
  $result = preg_replace('/\\s\\s/u', ' ', $result);
  return $result;
}
 
/* Вызовем функцию несколько раз */
$amount1 = mt_rand(1,999999999);
$text1 = numberToText($amount1);
 
echo "На вашем счету: {$text1}\n";
 
$amount2 = mt_rand(1,999999999);
$text2 = numberToText($amount2);
 
echo "На вашем счету: {$text2}\n";
 
$amount3 = mt_rand(1,999999999);
$text3 = numberToText($amount3);
 
echo "На вашем счету: {$text3}\n";
 
$amount4 = mt_rand(1,999999999);
$text4 = numberToText($amount4);
 
echo "На вашем счету: {$text4}\n";