<?php

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

$number = mt_rand(0,9999999);
    $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   =>  'две'
    );
    $menSpelling = array(
        1   =>  'один',        2   =>  'два'
    );
    $roubles = array(
    	'рубль' => array(1),
    	'рубля' => array(2, 3, 4),
    	'рублей' => array(0, 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)
    	);
    $thousands = array(
    	'тысяча' => array(1),
    	'тысячи' => array(2, 3, 4),
    	'тысяч' => array(0, 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)
    	);
    $millions = array(
    	'миллион' => array(1),
    	'миллиона' => array(2, 3, 4),
    	'миллионов' => array(0, 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)
    	);
    	
function findMatch($num, $spelling) {
	$result = 0;
foreach ($spelling as $numbers => $numbersInWords) {
    if ($num == $numbers) {
    	$result = $numbersInWords;
    	return $result;
    }
} 
}

function getRoubles($num, $roubles) {
foreach ($roubles as $forms => $numerals) {
    foreach ($numerals as $array => $numeral) {
    	if ($num == $numeral) {
    	return $forms;
    	}
    }	
} 
}
function threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $femaleSpelling) {
	foreach ($spelling as $numbers => $numbersInWords) {
        if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
    	$result = $numbersInWords;
    	$rouble = getRoubles($num, $roubles);
    	$i++;
        } 
	}
        if ($i > 0) {
        	$numeralsInWords = $result.' '.$rouble;
        	
        } else {
    	$firstNumeral = floor($num / 100); //находим первое число в сотне
    	$hundred = $firstNumeral * 100;
    	$num = $hundred;
        $hundredInWords = findMatch($num, $spelling);
        $ten = $number - $hundred; // Получаем десятки
        $num = $ten;
        if ($num == 0) {
	    } else {
	         foreach ($spelling as $numbers => $numbersInWords) {
                 if ($num == $numbers) {
    	         $result = $numbersInWords;
    	         if ($num == 1) {
        	           $result = findMatch($num, $femaleSpelling);
                   }
                   if ($num == 2) {
        	           $result = findMatch($num, $femaleSpelling);
                   }
    	         $rouble = getRoubles($num, $roubles);
    	         $x++;
    	        } 
             } 
	    }
             if ($x > 0) {
             	$numeralsInWords = $hundredInWords.' '.$result.' '.$rouble;
             } else {
             	   $tenWithoutOne = floor(($number - $hundred) / 10) * 10;
             	   $num = $tenWithoutOne;
             	   $tenWithoutOneInWords = findMatch($num, $spelling);
              	   $one = $number - ($hundred + $tenWithoutOne);
             	   $num = $one;
             	   $oneInWords = findMatch($num, $spelling);
             	   if ($num == 1) {
        	           $oneInWords = findMatch($num, $femaleSpelling);
                   }
                   if ($num == 2) {
        	           $oneInWords = findMatch($num, $femaleSpelling);
                   }
             	   $rouble = getRoubles($num, $roubles);
             	   $numeralsInWords = $hundredInWords.' '.$tenWithoutOneInWords.' '.$oneInWords.' '.$rouble;
             }
       }
    return $numeralsInWords; 
}
function twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $femaleSpelling) {
	foreach ($spelling as $numbers => $numbersInWords) {
        if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
    	$result = $numbersInWords;
    	if ($num == 1) {
        	$result = findMatch($num, $femaleSpelling);
            }
            if ($num == 2) {
        	$result = findMatch($num, $femaleSpelling);
            }
    	$rouble = getRoubles($num, $roubles);
    	$i++;
        } 
	}
        if ($i > 0) {
        	$numeralsInWords = $result.' '.$rouble;
        } else {
    	$firstNumeral = floor($num / 10); //находим первое число в десятке
    	$ten = $firstNumeral * 10;
    	$num = $ten;
        $tenInWords = findMatch($num, $spelling);
        $one = $number - $ten;
        $num = $one;
        $oneInWords = findMatch($num, $spelling);
        $result = $oneInWords;
            if ($num == 1) {
        	$oneInWords = findMatch($num, $femaleSpelling);
            }
            if ($num == 2) {
        	$oneInWords = findMatch($num, $femaleSpelling);
            }
        $rouble = getRoubles($num, $roubles);
        $numeralsInWords = $tenInWords.' '.$oneInWords.' '.$rouble;
        }
        return $numeralsInWords;
}
function oneNumeralToText($num, $number, $spelling, $roubles, $femaleSpelling) {
	foreach ($spelling as $numbers => $numbersInWords) {
        if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
    	$result = $numbersInWords;
    	$rouble = getRoubles($num, $roubles);
    	if ($num == 1) {
        	$result = findMatch($num, $femaleSpelling);
            }
            if ($num == 2) {
        	$result = findMatch($num, $femaleSpelling);
            }
        } 
	} 
	$numeralsInWords = $result.' '.$rouble;
	  return $numeralsInWords;
} 
$text = strval($number);
$count = mb_strlen($text);
$num = $number;
$i = 0;
$x = 0;
if ($count == 7) {
	$firstNumeral = floor($num / 1000000);
	$num = $firstNumeral;
	$firstNumeralInWords = findMatch($num, $spelling);
    $million = getRoubles($num, $millions);
	/*Ищет ноль в начале следующей тройки чисел*/
	$nextNumeral = floor(($number - ($firstNumeral * 1000000)) / 1000);
	if ($nextNumeral == 0) {
		$million = getRoubles($firstNumeral, $millions);
		$numeralsInWords = $firstNumeralInWords.' '.$million.' '.$rouble;
	} else {
		$text = strval($nextNumeral);
        $threeNumerals = mb_strlen($text);
        $temporary = $number;
        $num = $nextNumeral;
        $number = $nextNumeral;
        if ($threeNumerals == 3) {
        	$threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $femaleSpelling);
        	$number = $temporary;
        	$general = $threeNumeralsInWords;
            $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$threeNumeralsInWords;
        }
        if ($threeNumerals == 2) {
        	$twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $menSpelling);
        	$number = $temporary;
        	$general = $twoNumeralsInWords;
            $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$twoNumeralsInWords;
        }
        if ($threeNumerals == 1) {
        	$oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $thousands, $i, $x, $menSpelling);
        	$number = $temporary;
        	$general = $oneNumeralInWords;
            $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$oneNumeralInWords;
        }
	}
	$threeLastNumerals = $number - (($firstNumeral * 1000000) + ($nextNumeral * 1000));
	if ($threeLastNumerals == 0) {
		$rouble = getRoubles($nextNumeral, $roubles);
		$number = $temporary;
		if ($threeLastNumerals != $nextNumeral) {
			$numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$rouble;
		}
		if ($threeLastNumerals == $nextNumeral) {
			$numeralsInWords = $firstNumeralInWords.' '.$million.' '.$rouble;
		}
		
	} else {
		$text = strval($threeLastNumerals);
        $threeNumerals = mb_strlen($text);
        $num = $threeLastNumerals;
        $temporary = $number;
        $number = $threeLastNumerals;
        if ($threeNumerals == 3) {
        	$threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
        	if ($nextNumeral != 0) {
            $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$threeNumeralsInWords;
		    }
		    if ($nextNumeral == 0) {
			$numeralsInWords = $firstNumeralInWords.' '.$million.' '.$threeNumeralsInWords;
		    }
        }
        if ($threeNumerals == 2) {
        	$twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
        	if ($nextNumeral != 0) {
            $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$twoNumeralsInWords;
		    }
		    if ($nextNumeral == 0) {
			$numeralsInWords = $firstNumeralInWords.' '.$million.' '.$twoNumeralsInWords;
		    }
        }
        if ($threeNumerals == 1) {
        	$oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
            if ($nextNumeral != 0) {
            $number = $temporary;
            $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$oneNumeralInWords;
		    }
		    if ($nextNumeral == 0) {
		    $number = $temporary;
			$numeralsInWords = $firstNumeralInWords.' '.$million.' '.$oneNumeralInWords;
		    }
        }
	}
}	
if ($count == 6) {
	$threeNumerals = floor($num / 1000);
	$num = $threeNumerals;
	$temporary = $number;
	$number = $threeNumerals;
	$threeNumeralsToText = threeNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $femaleSpelling);
	/*Ищет ноль в начале следующей тройки чисел*/
	$nextNumeral = $temporary - ($threeNumerals * 1000);
	if ($nextNumeral == 0) {
		$rouble = getRoubles($nextNumeral, $roubles);
		$number = $temporary;
		$numeralsInWords = $threeNumeralsToText.' '.$rouble;
	} else {
		$text = strval($nextNumeral);
        $threeNumerals = mb_strlen($text);
        $num = $nextNumeral;
        $number = $nextNumeral;
        if ($threeNumerals == 3) {
        	$threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
            $numeralsInWords = $threeNumeralsToText.' '.$threeNumeralsInWords;
        }
        if ($threeNumerals == 2) {
        	$twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
            $numeralsInWords = $threeNumeralsToText.' '.$twoNumeralsInWords;
        }
        if ($threeNumerals == 1) {
        	$oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
            $number = $temporary;
            $numeralsInWords = $threeNumeralsToText.' '.$oneNumeralInWords;
        }
	}
}	
if ($count == 5) {
	$twoNumerals = floor($num / 1000);
	$num = $twoNumerals;
	$temporary = $number;
	$number = $twoNumerals;
	$thousandsInWords = twoNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $femaleSpelling);
	/*Ищет ноль в начале следующей тройки чисел*/
	$nextNumeral = $temporary - ($twoNumerals * 1000);
	if ($nextNumeral == 0) {
		$rouble = getRoubles($nextNumeral, $roubles);
		$number = $temporary;
		$numeralsInWords = $thousandsInWords.' '.$rouble;
	} else {
		$text = strval($nextNumeral);
        $threeNumerals = mb_strlen($text);
        $num = $nextNumeral;
        $number = $nextNumeral;
        if ($threeNumerals == 3) {
        	$threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x);
        	$number = $temporary;
            $numeralsInWords = $thousandsInWords.' '.$threeNumeralsInWords;
        }
        if ($threeNumerals == 2) {
        	$twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
            $numeralsInWords = $thousandsInWords.' '.$twoNumeralsInWords;
        }
        if ($threeNumerals == 1) {
        	$oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
            $number = $temporary;
            $numeralsInWords = $thousandsInWords.' '.$oneNumeralInWords;
        }
	}
}	
if ($count == 4) {
	$firstNumeral = floor($num / 1000);
	$num = $firstNumeral;
	$firstNumeralInWords = findMatch($num, $spelling);
	    if ($num == 1) {
        	$firstNumeralInWords = findMatch($num, $femaleSpelling);
        }
        if ($num == 2) {
        	$firstNumeralInWords = findMatch($num, $femaleSpelling);
        }
    $thousand = getRoubles($num, $thousands);
	/*Ищет ноль в начале следующей тройки чисел*/
	$nextNumeral = $number - ($firstNumeral * 1000);
	if ($nextNumeral == 0) {
		$rouble = getRoubles($nextNumeral, $roubles);
		$numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$rouble;
	} else {
		$text = strval($nextNumeral);
        $threeNumerals = mb_strlen($text);
        $temporary = $number;
        $num = $nextNumeral;
        $number = $nextNumeral;
        if ($threeNumerals == 3) {
        	$threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
            $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$threeNumeralsInWords;
        }
        if ($threeNumerals == 2) {
        	$twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
            $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$twoNumeralsInWords;
        }
        if ($threeNumerals == 1) {
        	$oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
        	$number = $temporary;
            $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$oneNumeralInWords;
        }
	}
}	
if ($count == 3) {
    $numeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
    
}
if ($count == 2) {
	$numeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
}
if ($count == 1) {
    $numeralsInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
}        
        	
echo "На счету \"$number руб.\" или \"$numeralsInWords\".";