<?php

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

$number = 121;
//$number = mt_rand(0,999);
    $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   =>  'две'
    );
    $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)
    	);
/*function findMatches($num, $spelling) {
    $result = 0;
    foreach ($spelling as $keys => $values) {
        if ($num == $keys) {
	        $result = $values;
	        break;
        } 
    } return $result;
}*/
function findThousandMatches($num, $spelling, $femaleSpelling) {
    $result = 0;
    foreach ($spelling as $keys => $values) {
        foreach ($femaleSpelling as $key => $value) {
	    		if ($num == $key) {
	                $result = $value;
	                break;
	            } elseif ($num == $keys) {
	    	        $result = $values;
                  } 
	   }
    } return $result;
}
function findThousandsForm($num, $thousands) {
	$thousand = 0;
    foreach ($thousands as $form => $numerals) {
	    foreach ($numerals as $s => $numeral) {
	    	if ($num == $numeral) {
		    $thousand = $form;
		    }
	    }
    } return $thousand;
}	
function smallNumberToText($number, $spelling, $thousands, $femaleSpelling) {
$i = 0;
$o = 0;
$text = strval($number);
$countLetters = mb_strlen($text);

foreach ($spelling as $keys => $values) {
    if ($number == $keys) {
	    $splash = $values;
	    $o++;
	    break;
    } 
}
$num = $number;
foreach ($spelling as $keys => $values) {
        foreach ($femaleSpelling as $key => $value) {
	    		if ($num == $key) {
	                $splash = $value;
	                break;
	                
	                
	    	    } elseif ($num == $keys) {
	    	        $splash = $values;
	    	        
	    	      } 
	   }
    }
$thousand = findThousandsForm($num, $thousands);
if ($o > 0) {
            $numberInWords = $splash.' '.$thousand;
} else {
        if ($countLetters == 3) {
        $firstNumberOfTheHundred = mb_substr($text, 0, 1);
        $findHundredEqual = (int)$firstNumberOfTheHundred * 100;
            if ($findHundredEqual == 0) {
            } else {
                    $num = $findHundredEqual;
                    $hundred = findThousandMatches($num, $spelling, $femaleSpelling);
                    $thousand = findThousandsForm($num, $thousands);
              } 
        $findLastTwoNumerals = mb_substr($text, 1, 2);
        $lastTwoNumerals = (int)$findLastTwoNumerals;
        $num = $lastTwoNumerals;
        foreach ($spelling as $keys => $values) {
            foreach ($femaleSpelling as $key => $value) {
	    		if ($num == $key) {
	                $ten = $value;
	                $i++;
	                break;
	            } elseif ($num == $keys) {
	    	        $ten = $values;
	    	        $i++;
	    	      } 
	        }
        }
	    
        $thousand = findThousandsForm($num, $thousands);
        if ($i > 0) {
            $numberInWords = $hundred.' '.$ten.' '.$thousand;
        } else {
        $firstNumberOfTheTen = mb_substr($text, 1, 1);
        $findTenEqual = (int)$firstNumberOfTheTen * 10;
        if ($findTenEqual == 0) {
        } else {
        	$num = $findTenEqual;
        	$ten = findThousandMatches($num, $spelling, $femaleSpelling);
            
          }
        $lastOneNumeral = mb_substr($text, 2, 1);
        $findLastOneNumeral = (int)$lastOneNumeral;
        $num = $findLastOneNumeral;
        
        $one = findThousandMatches($num, $spelling, $femaleSpelling);
        $thousand = findThousandsForm($num, $thousands);
        $numberInWords = $hundred.' '.$ten.' '.$one.' '.$thousand;
         }
        }
        
        if ($countLetters == 2) {
        	$i = 0;
        	    $lastTwoNumerals = mb_substr($text, 0, 2);
                $twoNumerals = (int)$lastTwoNumerals;
                foreach ($spelling as $keys => $values) {
                    if ($twoNumerals == $keys) {
	                $ten = $values;
	                $i++;
	                break;
                    } 
                }
                $num = $twoNumerals;
                $thousand = findThousandsForm($num, $thousands);
        if ($i > 0) {
            $numberInWords = $hundred.' '.$ten.' '.$thousand; 
        } else {
        $firstNumeralOfTheTen = mb_substr($text, 0, 1);
        $tenEqual = (int)$firstNumeralOfTheTen * 10; 
        if ($tenEqual == 0) {
        } else {
        	$num = $tenEqual;
        	$ten = findThousandMatches($num, $spelling, $femaleSpelling);
          }
        $lastNumeral = mb_substr($text, 1, 1);
        $findLastNumeral = (int)$lastNumeral;
        $num = $findLastNumeral;
        var_dump($num);
        $one = findThousandMatches($num, $spelling, $femaleSpelling);
        var_dump($one);
        $thousand = findThousandsForm($num, $thousands);
        $numberInWords = $ten.' '.$one.' '.$thousand;
          }
        }
    }
    return $numberInWords;
}

//$number = mt_rand(1,999);
$cheking = smallNumberToText($number, $spelling, $thousands, $femaleSpelling);
echo "\"{$number}\" - это \"{$cheking}\".";

?>