<?php

$input = "110022";
$counter = $input;
$male_or_female_unit = 1;
$number = '';
$digit_1 = '';
$digit_2 = '';
$digitTotal = '';
$output = '';

function writeNumber() {
	global $number, $input;
	
	if (strlen($input) < 4) {
		$number = $input;
	} elseif ((strlen($input) % 3) == 0) {
		$number = $input[0] . $input[1] . $input[2];
	} else {
		$number = substr($input, 0, (strlen($input) % 3));
	}
} 

function insertDigit() {
	global $output, $digitTotal, $digit_1, $digit_2, $number;
	$digitArray_1 = array("/0a$/", "/0b$/", "/0c$/", "/1a$/", "/1b$/", "/1c$/", "/2a$/", "/2b$/", "/2c$/", "/3a$/", "/3b$/", "/3c$/");
	$digitArray_2 = array("рубль", "рубля", "рублей", "тысяча", "тысячи", "тысяч", "миллион", "миллиона", "миллионов", "миллиард", "миллиарда", "миллиардов");
	$digitTotal = $digit_1 . $digit_2;
	if (($number == "000") && ($digit_1 != "0")) {
		$digitTotal = "";
	}
	$output = $output . " " . $digitTotal;
	$output = preg_replace($digitArray_1, $digitArray_2, $output);
}

function checkDigit() {
	global $digit_1, $input, $male_or_female_unit;
	if ((1 <= strlen($input)) && (3 >= strlen($input))) {       //0 - первая тройка, 1 - тысячи, 2 - мульёны, 3 - мульярды
		$digit_1 = "0";
	} elseif ((4 <= strlen($input)) && (6 >= strlen($input))) {
		$digit_1 = "1";
		$male_or_female_unit = 0;
	} elseif ((7 <= strlen($input)) && (9 >= strlen($input))) {
		$digit_1 = "2";
	} elseif ((10 <= strlen($input)) && (12 >= strlen($input))) {
		$digit_1 = "3";
	}
}

function subnumber($x) {
	global $digit_2;
	
	if (substr($x, -2, 2) == "00") {
		$digit_2 = "c";
	} elseif ($x[-1] == "1") {
		$digit_2 = "a";
	} elseif (($x[-1] == "2") || ($x[-1] == "3") || ($x[-1] == "4")) {
		$digit_2 = "b";
	} else {
		$digit_2 = "c";
	}
	
	if (strlen($x) == 3) {
		hundreds($x);
		(preg_match('/(?<=\d)[1]\d/', $x)) ? ifTeenDecimals($x) : ifNotTeenDecimals($x);
	} elseif (strlen($x) == 1) {
		units($x);
	} else {
		(preg_match('/[1]\d/', $x)) ? ifTeenDecimals($x) : ifNotTeenDecimals($x);
	}
}

function hundreds($x) {
	global $output;
	$numberReplace = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
	$hundredWords = array("", "cто", "двести", "триста", "четыреста", "пятьсот", "шестьсот", "семьсот", "восемьсот", "девятьсот");
	$output = $output . str_replace($numberReplace, $hundredWords, $x[0]) . " ";
	return $output;
}

function ifTeenDecimals($x) {
	global $output, $digit_2;
	$numberReplace = array("10", "11", "12", "13", "14", "15", "16", "17", "18", "19");
	$decimalWords = array("десять", "одиннадцать", "двенадцать", "тринадцать", "четырнадцать", "пятнадцать", "шестнадцать", "семнадцать", "восемнадцать", "девятнадцать");
	$output = $output . str_replace($numberReplace, $decimalWords, substr($x, -2, 2));
	$digit_2 = "c";
	return $output;
}
	
function units($x) {
	global $output, $male_or_female_unit, $digit_2;
	
	if (($x[-1] != 1) && ($x[-1] != 2)) {
		$numberReplace = array("0", "3", "4", "5", "6", "7", "8", "9");
		$unitsWords = array("", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять");
		$output = $output . str_replace($numberReplace, $unitsWords, $x[-1]);
		return $output;	
	} else {
		if ($male_or_female_unit == 1) {
			$output = $output . str_replace(array("1", "2"), array("один", "два"), $x[-1]);
			return $output;
		} else {
			$output = $output . str_replace(array("1", "2"), array("одна", "две"), $x[-1]);
			return $output;
		}
	}
}

function ifNotTeenDecimals($x) {
	global $output;
	$numberReplace = array("0", "2", "3", "4", "5", "6", "7", "8", "9");
	$decimalWords = array("", "двадцать", "тридцать", "сорок", "пятьдесят", "шестьдесят", "семьдесят", "восемьдесят", "девяносто");
	$output = $output . str_replace($numberReplace, $decimalWords, $x[-2]) . " ";
	return units($x);
}

function computePart() {
	global $input, $number, $male_or_female_unit;
	$male_or_female_unit = 1;
	writeNumber();
	checkDigit();
	subnumber($number);
	insertDigit();
	$input = substr($input, strlen($number));
}

for ($i = 0; floor(strlen($counter) / 3) >= $i; $i++) {
	computePart();
	$output = $output . " ";
}
$output = preg_replace(array("/\s+/", "/(рубл[яейь]+)(\s+)рублей/u"), array(" ", "$1"), $output);

echo "На вашем счету $output(" . $counter . " р.)";

?>