<?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(preg_match("/^1$|21|31|41|51|61|71|81|91/",$last2Digits)){
  if(preg_match("/^1$|21|31|41|51|61|71|81|91/",$last2Digits) && $word1=="тысяча"){ $numberEnd = smallNumberToText($number,1)." {$word1}";}
  else{$numberEnd = smallNumberToText($number,0)." {$word1}";}
}
else if(preg_match("/^[2-4]$|2[2-4]|3[2-4]|4[2-4]|5[2-4]|6[2-4]|7[2-4]|8[2-4]|9[2-4]/",$last2Digits))
{
  if(preg_match("/^2$|22|32|42|52|62|72|82|92/",$last2Digits) && $word2=="тысячи"){$numberEnd = smallNumberToText($number,1)." {$word2}";}
  else{$numberEnd = smallNumberToText($number,0)." {$word2}";}
}
else if(preg_match("/^[5-9]$|0|[1-9]0|1[0-9]|2[5-9]|3[5-9]|4[5-9]|5[5-9]|6[5-9]|7[5-9]|8[5-9]|9[5-9]/",$last2Digits))
{
$numberEnd = smallNumberToText($number,0)." {$word5}";
}

return $numberEnd;
}
 
/*
  Преобразует числа от 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 => 'две'
);

$thousand = floor($number/100)*100;

if(($number-$thousand)<10 || ($number-$thousand)>19){
$ten = floor(($number-$thousand)/10)*10;
}else{
$ten = $number-$thousand;
}
$oneUnit = $number-$thousand-$ten;

if($thousand==0){
$thousandText = "";
}else{
$thousandText = $spelling[$thousand];
}

if($ten==0){
$tenText = "";
}else{
$tenText = $spelling[$ten];
}

if($oneUnit==0){
$oneUnitText = "";
}else{
if($isFemale==1){
$oneUnitText = $femaleSpelling[$oneUnit];
}else{
$oneUnitText = $spelling[$oneUnit];
}
}

return $thousandText." ".$tenText." ".$oneUnitText;
}
 
function numberToText($number) {
$millionsText="";
$thousandsText="";
$hundredsText="";
if($number == 0){
  return "ноль рублей";
}
$millions = floor($number / 1000000);
if($millions!=0){
$millionsText = inclineWord($millions,"миллион","миллиона","миллионов");
}

$thousands = floor(($number - $millions*1000000) / 1000);
if($thousands!=0){
$thousandsText = inclineWord($thousands,"тысяча","тысячи","тысячь");
}

$hundreds = $number - ($millions*1000000) - ($thousands*1000);
if($hundreds!=0){
$hundredsText = inclineWord($hundreds,"рубль","рубля","рублей");
}

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