fork download
  1. <?php
  2.  
  3.  
  4. $number = 121;
  5. //$number = mt_rand(0,999);
  6. $spelling = array(
  7. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  8. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  9. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  10. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  11. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  12. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  13. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  14. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  15. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  16. 9 => 'девять', 19 => 'девятнадцать'
  17. );
  18. $femaleSpelling = array(
  19. 1 => 'одна', 2 => 'две'
  20. );
  21. $roubles = array(
  22. 'рубль' => array(1),
  23. 'рубля' => array(2, 3, 4),
  24. 'рублей' => 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)
  25. );
  26. $thousands = array(
  27. 'тысяча' => array(1),
  28. 'тысячи' => array(2, 3, 4),
  29. 'тысяч' => 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)
  30. );
  31. /*function findMatches($num, $spelling) {
  32.   $result = 0;
  33.   foreach ($spelling as $keys => $values) {
  34.   if ($num == $keys) {
  35. $result = $values;
  36. break;
  37.   }
  38.   } return $result;
  39. }*/
  40. function findThousandMatches($num, $spelling, $femaleSpelling) {
  41. $result = 0;
  42. foreach ($spelling as $keys => $values) {
  43. foreach ($femaleSpelling as $key => $value) {
  44. if ($num == $key) {
  45. $result = $value;
  46. break;
  47. } elseif ($num == $keys) {
  48. $result = $values;
  49. }
  50. }
  51. } return $result;
  52. }
  53. function findThousandsForm($num, $thousands) {
  54. $thousand = 0;
  55. foreach ($thousands as $form => $numerals) {
  56. foreach ($numerals as $s => $numeral) {
  57. if ($num == $numeral) {
  58. $thousand = $form;
  59. }
  60. }
  61. } return $thousand;
  62. }
  63. function smallNumberToText($number, $spelling, $thousands, $femaleSpelling) {
  64. $i = 0;
  65. $o = 0;
  66. $text = strval($number);
  67. $countLetters = mb_strlen($text);
  68.  
  69. foreach ($spelling as $keys => $values) {
  70. if ($number == $keys) {
  71. $splash = $values;
  72. $o++;
  73. break;
  74. }
  75. }
  76. $num = $number;
  77. foreach ($spelling as $keys => $values) {
  78. foreach ($femaleSpelling as $key => $value) {
  79. if ($num == $key) {
  80. $splash = $value;
  81. break;
  82.  
  83.  
  84. } elseif ($num == $keys) {
  85. $splash = $values;
  86.  
  87. }
  88. }
  89. }
  90. $thousand = findThousandsForm($num, $thousands);
  91. if ($o > 0) {
  92. $numberInWords = $splash.' '.$thousand;
  93. } else {
  94. if ($countLetters == 3) {
  95. $firstNumberOfTheHundred = mb_substr($text, 0, 1);
  96. $findHundredEqual = (int)$firstNumberOfTheHundred * 100;
  97. if ($findHundredEqual == 0) {
  98. } else {
  99. $num = $findHundredEqual;
  100. $hundred = findThousandMatches($num, $spelling, $femaleSpelling);
  101. $thousand = findThousandsForm($num, $thousands);
  102. }
  103. $findLastTwoNumerals = mb_substr($text, 1, 2);
  104. $lastTwoNumerals = (int)$findLastTwoNumerals;
  105. $num = $lastTwoNumerals;
  106. foreach ($spelling as $keys => $values) {
  107. foreach ($femaleSpelling as $key => $value) {
  108. if ($num == $key) {
  109. $ten = $value;
  110. $i++;
  111. break;
  112. } elseif ($num == $keys) {
  113. $ten = $values;
  114. $i++;
  115. }
  116. }
  117. }
  118.  
  119. $thousand = findThousandsForm($num, $thousands);
  120. if ($i > 0) {
  121. $numberInWords = $hundred.' '.$ten.' '.$thousand;
  122. } else {
  123. $firstNumberOfTheTen = mb_substr($text, 1, 1);
  124. $findTenEqual = (int)$firstNumberOfTheTen * 10;
  125. if ($findTenEqual == 0) {
  126. } else {
  127. $num = $findTenEqual;
  128. $ten = findThousandMatches($num, $spelling, $femaleSpelling);
  129.  
  130. }
  131. $lastOneNumeral = mb_substr($text, 2, 1);
  132. $findLastOneNumeral = (int)$lastOneNumeral;
  133. $num = $findLastOneNumeral;
  134.  
  135. $one = findThousandMatches($num, $spelling, $femaleSpelling);
  136. $thousand = findThousandsForm($num, $thousands);
  137. $numberInWords = $hundred.' '.$ten.' '.$one.' '.$thousand;
  138. }
  139. }
  140.  
  141. if ($countLetters == 2) {
  142. $i = 0;
  143. $lastTwoNumerals = mb_substr($text, 0, 2);
  144. $twoNumerals = (int)$lastTwoNumerals;
  145. foreach ($spelling as $keys => $values) {
  146. if ($twoNumerals == $keys) {
  147. $ten = $values;
  148. $i++;
  149. break;
  150. }
  151. }
  152. $num = $twoNumerals;
  153. $thousand = findThousandsForm($num, $thousands);
  154. if ($i > 0) {
  155. $numberInWords = $hundred.' '.$ten.' '.$thousand;
  156. } else {
  157. $firstNumeralOfTheTen = mb_substr($text, 0, 1);
  158. $tenEqual = (int)$firstNumeralOfTheTen * 10;
  159. if ($tenEqual == 0) {
  160. } else {
  161. $num = $tenEqual;
  162. $ten = findThousandMatches($num, $spelling, $femaleSpelling);
  163. }
  164. $lastNumeral = mb_substr($text, 1, 1);
  165. $findLastNumeral = (int)$lastNumeral;
  166. $num = $findLastNumeral;
  167. var_dump($num);
  168. $one = findThousandMatches($num, $spelling, $femaleSpelling);
  169. var_dump($one);
  170. $thousand = findThousandsForm($num, $thousands);
  171. $numberInWords = $ten.' '.$one.' '.$thousand;
  172. }
  173. }
  174. }
  175. return $numberInWords;
  176. }
  177.  
  178. //$number = mt_rand(1,999);
  179. $cheking = smallNumberToText($number, $spelling, $thousands, $femaleSpelling);
  180. echo "\"{$number}\" - это \"{$cheking}\".";
  181.  
  182. ?>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
"121" - это "сто двадцать одна тысяча".