<?php

error_reporting(-1);
mb_internal_encoding('utf-8');

function spellSmallNumber($int, $isMan) {
	if ($int >= 0 && $int < 1000) {
		$spelling = [
			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 => 'девятьсот',
			];
			
		$spellingWomen = [
				1 => 'одна',
				2 => 'две'
			];

		if($int/100 >= 1) {
			$word = 100 * floor($int/100);
			$word = $spelling[$word];
			$result = $word;
			if($int % 100 >= 1) {
				if($isMan == 0 && ($int % 100 == 1 || $int % 100 == 2)) {
					$word1 = $spellingWomen[$int % 100];
					$result = $result.' '.$word1;
				} elseif($int % 100 <= 19 || $int % 100 % 10 == 0) {
					$word1 = $spelling[$int % 100];
					$result = $result.' '.$word1;
				} else {
					$word1 = 10 * floor($int % 100 / 10);
					$word1 = $spelling[$word1];
					if($isMan == 0 && ($int % 100 % 10 == 1 ||$int % 100 % 10 == 2)) {
						$word2 = $int % 100 % 10;
						$word2 = $spellingWomen[$word2];
					} else {
						$word2 = $int % 100 % 10;
						$word2 = $spelling[$word2];
					}
					$result = $word.' '.$word1.' '.$word2;
				}
			}
		} else {
				if(($int >= 0 && $int <= 19) || $int % 10 == 0) {
					if($isMan == 0 && ($int == 1 || $int == 2)) {
						$word = $spellingWomen[$int];
						$result = $word;
					} else {
						$word = $spelling[$int];
						$result = $word;
					}
				} else {
					$word = 10 * floor($int / 10);
					$word = $spelling[$word];
					$word1 = $int % 10;
					$word1 = $spelling[$word1];
					$result = $word.' '.$word1;
				}
			}
	return $result;
	}
}

function getWordForm($int, $str, $str2, $str5) {
	if($int == 0 || ($int >= 5 && $int <= 20) || $int % 10 >= 5 || $int % 10 == 0 || ($int % 100 <= 19 && $int % 100 >= 5)) {
		return($str5);
	}
	
	if(($int >= 2 && $int <= 4) || ($int % 10 >= 2 && $int % 10 <= 4)) {
		return($str2);
	}
	
	if($int == 1 || $int % 10 == 1) {
		return($str);
	}
}

function spellNumber($int) {
	if($int >=1000000 && $int <= 999999999) {
		$millions = floor($int / 1000000);
		$smallNumberWord = spellSmallNumber($millions, 1);
		$thousands = floor($int / 1000) % 1000;
		$smallNumberWord1 = spellSmallNumber($thousands, 0);
		$smallNumberWord2 = $int % 1000;
		$smallNumberWord2 = spellSmallNumber($smallNumberWord2, 1);
		$numberForm = getWordForm($int, 'рубль', 'рубля', 'рублей');
		$numberForm1 = getWordForm($thousands ,'тысяча', 'тысячи', 'тысяч');
		$numberForm2 = getWordForm($millions, 'миллион', 'миллиона', 'миллионов');
		$result = $smallNumberWord.' '.$numberForm2.' '.$smallNumberWord1.' '.$numberForm1.' '.$smallNumberWord2.' '.$numberForm;
		$result = preg_replace('/ноль /u', '', $result);
		$result = preg_replace('/(?<=миллионов )тысяч /u', '', $result);
	} elseif($int >= 1000 && $int <= 999999) {
		$thousands = floor($int / 1000);
		$smallNumberWord = spellSmallNumber($thousands, 0);
		$smallNumberWord1 = $int % 1000;
		$smallNumberWord1 = spellSmallNumber($smallNumberWord1, 1);
		$numberForm = getWordForm($int, 'рубль', 'рубля', 'рублей');
		$numberForm1 = getWordForm($thousands ,'тысяча', 'тысячи', 'тысяч');
		$result = $smallNumberWord.' '.$numberForm1.' '.$smallNumberWord1.' '.$numberForm;
		$result = preg_replace('/ноль /u', '', $result);
	} else {
		$smallNumberWord = spellSmallNumber($int, 1);
		$numberForm = getWordForm($int, 'рубль', 'рубля', 'рублей');
		$result = $smallNumberWord.' '.$numberForm;
	}
	return $result;
}

$amount =  mt_rand(0, 999999999);
echo "[$amount] ";
$text = spellNumber($amount);
echo "На банковском счету клиента №1 - $text\n";

$amount1 = mt_rand(0, 999999999);
echo "[$amount1] ";
$text1 = spellNumber($amount1);
echo "На банковском счету клиента №2 - $text1\n";

$amount2 = mt_rand(0, 999999999);
echo "[$amount2] ";
$text2 = spellNumber($amount2);
echo "На банковском счету клиента №3 - $text2\n";

$amount3 = mt_rand(0, 999999999);
echo "[$amount3] ";
$text3 = spellNumber($amount3);
echo "На банковском счету клиента №4 - $text3\n";

$amount4 = 999999999;
echo "[$amount4] ";
$text4 = spellNumber($amount4);
echo "На банковском счету клиента №5 - $text4\n";

$amount5 = 0;
echo "[$amount5] ";
$text5 = spellNumber($amount5);
echo "На банковском счету клиента №6 - $text5\n";

$amount6 = 1;
echo "[$amount6] ";
$text6 = spellNumber($amount6);
echo "На банковском счету клиента №7 - $text6\n";

$amount7 = 11012013;
echo "[$amount7] ";
$text7 = spellNumber($amount7);
echo "На банковском счету клиента №8 - $text7\n";

$amount8 = 7000008;
echo "[$amount8] ";
$text8 = spellNumber($amount8);
echo "На банковском счету клиента №9 - $text8\n";

$amount9 = 1002;
echo "[$amount9] ";
$text9 = spellNumber($amount9);
echo "На банковском счету клиента №10 - $text9\n";

$amount10 = 1000;
echo "[$amount10] ";
$text10 = spellNumber($amount10);
echo "На банковском счету клиента №11 - $text10\n";

$amount11 = 7000000;
echo "[$amount11] ";
$text11 = spellNumber($amount11);
echo "На банковском счету клиента №12 - $text11\n";
