fork download
  1. <?php
  2.  
  3.  
  4. function findMatch($num, $spelling) {
  5. $result = 0;
  6. foreach ($spelling as $numbers => $numbersInWords) {
  7. if ($num == $numbers) {
  8. $result = $numbersInWords;
  9. return $result;
  10. }
  11. }
  12. }
  13. function getEnding($num, $roubles) {
  14. foreach ($roubles as $forms => $numerals) {
  15. foreach ($numerals as $array => $numeral) {
  16. if ($num == $numeral) {
  17. return $forms;
  18. }
  19. }
  20. }
  21. }
  22. function smallNumberToText($num, $number, $spelling, $roubles, $femaleSpelling) {
  23. $text = strval($number);
  24. $count = mb_strlen($text);
  25. $x = 0;
  26. $y = 0;
  27. $num = $number;
  28. if ($count == 3) {
  29. foreach ($spelling as $numbers => $numbersInWords) {
  30. if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
  31. $result = $numbersInWords;
  32. $rouble = getEnding($num, $roubles);
  33. $y++;
  34. }
  35. }
  36. if ($y > 0) {
  37. $numeralsInWords = $result.' '.$rouble;
  38. } else {
  39. $firstNumeral = floor($num / 100); //находим первое число в сотне
  40. $hundred = $firstNumeral * 100;
  41. $num = $hundred;
  42. $hundredInWords = findMatch($num, $spelling);
  43. $ten = $number - $hundred; // Получаем десятки
  44. $num = $ten;
  45. if ($num == 0) {
  46. } else {
  47. foreach ($spelling as $numbers => $numbersInWords) {
  48. if ($num == $numbers) {
  49. $result = $numbersInWords;
  50. if ($num == 1) {
  51. $result = findMatch($num, $femaleSpelling);
  52. }
  53. if ($num == 2) {
  54. $result = findMatch($num, $femaleSpelling);
  55. }
  56. $rouble = getEnding($num, $roubles);
  57. $x++;
  58. }
  59. }
  60. }
  61. if ($x > 0) {
  62. $numeralsInWords = $hundredInWords.' '.$result.' '.$rouble;
  63. } else {
  64. $tenWithoutOne = floor(($number - $hundred) / 10) * 10;
  65. $num = $tenWithoutOne;
  66. $tenWithoutOneInWords = findMatch($num, $spelling);
  67. $one = $number - ($hundred + $tenWithoutOne);
  68. $num = $one;
  69. $oneInWords = findMatch($num, $spelling);
  70. if ($num == 1) {
  71. $oneInWords = findMatch($num, $femaleSpelling);
  72. }
  73. if ($num == 2) {
  74. $oneInWords = findMatch($num, $femaleSpelling);
  75. }
  76. $rouble = getEnding($num, $roubles);
  77. $numeralsInWords = $hundredInWords.' '.$tenWithoutOneInWords.' '.$oneInWords.' '.$rouble;
  78. }
  79. }
  80. return $numeralsInWords;
  81. }
  82. if ($count == 2) {
  83. foreach ($spelling as $numbers => $numbersInWords) {
  84. if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
  85. $result = $numbersInWords;
  86. $rouble = getEnding($num, $roubles);
  87. $y++;
  88. }
  89. }
  90. if ($y > 0) {
  91. $numeralsInWords = $result.' '.$rouble;
  92. } else {
  93. $firstNumeral = floor($num / 10); //находим первое число в десятке
  94. $ten = $firstNumeral * 10;
  95. $num = $ten;
  96. $tenInWords = findMatch($num, $spelling);
  97. $one = $number - $ten;
  98. $num = $one;
  99. $oneInWords = findMatch($num, $spelling);
  100. $result = $oneInWords;
  101. if ($num == 1) {
  102. $oneInWords = findMatch($num, $femaleSpelling);
  103. }
  104. if ($num == 2) {
  105. $oneInWords = findMatch($num, $femaleSpelling);
  106. }
  107. $rouble = getEnding($num, $roubles);
  108. $numeralsInWords = $tenInWords.' '.$oneInWords.' '.$rouble;
  109. }
  110. return $numeralsInWords;
  111. }
  112. if ($count == 1) {
  113. foreach ($spelling as $numbers => $numbersInWords) {
  114. if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
  115. $result = $numbersInWords;
  116. $rouble = getEnding($num, $roubles);
  117. if ($num == 1) {
  118. $result = findMatch($num, $femaleSpelling);
  119. }
  120. if ($num == 2) {
  121. $result = findMatch($num, $femaleSpelling);
  122. }
  123. }
  124. }
  125. $numeralsInWords = $result.' '.$rouble;
  126. return $numeralsInWords;
  127. }
  128. }
  129. function numberToText($number) {
  130. $spelling = array(
  131. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  132. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  133. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  134. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  135. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  136. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  137. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  138. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  139. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  140. 9 => 'девять', 19 => 'девятнадцать'
  141. );
  142. $femaleSpelling = array(
  143. 1 => 'одна', 2 => 'две'
  144. );
  145. $menSpelling = array(
  146. 1 => 'один', 2 => 'два'
  147. );
  148. $roubles = array(
  149. 'рубль' => array(1),
  150. 'рубля' => array(2, 3, 4),
  151. 'рублей' => 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)
  152. );
  153. $thousands = array(
  154. 'тысяча' => array(1),
  155. 'тысячи' => array(2, 3, 4),
  156. 'тысяч' => 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)
  157. );
  158. $millions = array(
  159. 'миллион' => array(1),
  160. 'миллиона' => array(2, 3, 4),
  161. 'миллионов' => 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)
  162. );
  163. $text = strval($number);
  164. $count = mb_strlen($text);
  165. if ($count == 9 || $count == 8 || $count == 7) {
  166. $num = $number;
  167. $temporaryNumber = $number;
  168. $millionsNumeral = floor($num / 1000000);
  169. $num = $millionsNumeral;
  170. $number = $millionsNumeral;
  171. $millionsNumeralInWords = smallNumberToText($num, $number, $spelling, $millions, $menSpelling);
  172. $number = $temporaryNumber;
  173. $thousandsNumerals = floor(($number - ($millionsNumeral * 1000000)) / 1000);
  174. $lastThreeNumerals = floor($number % 1000);
  175. if ($thousandsNumerals == 0) {
  176. if ($lastThreeNumerals == 0) {
  177. $rouble = getEnding($thousandsNumerals, $roubles);
  178. $numeralsInWords = $millionsNumeralInWords.' '.$rouble;
  179. } else {
  180. $temporaryNumber = $number;
  181. $num = $lastThreeNumerals;
  182. $number = $lastThreeNumerals;
  183. $lastThreeNumeralsInWords = smallNumberToText($num, $number, $spelling, $roubles, $menSpelling);
  184. $number = $temporaryNumber;
  185. $numeralsInWords = $millionsNumeralInWords.' '.$lastThreeNumeralsInWords.' '.$rouble;
  186. }
  187. } else {
  188. $temporaryNumber = $number;
  189. $num = $thousandsNumerals;
  190. $number = $thousandsNumerals;
  191. $thousandsNumeralsInWords = smallNumberToText($num, $number, $spelling, $thousands, $femaleSpelling);
  192. $number = $temporaryNumber;
  193. if ($lastThreeNumerals == 0) {
  194. $rouble = getEnding($lastThreeNumerals, $roubles);
  195. $numeralsInWords = $millionsNumeralInWords.' '.$thousandsNumeralsInWords.' '.$rouble;
  196. } else {
  197. $temporaryNumber = $number;
  198. $num = $lastThreeNumerals;
  199. $number = $lastThreeNumerals;
  200. $lastThreeNumeralsInWords = smallNumberToText($num, $number, $spelling, $roubles, $menSpelling);
  201. $number = $temporaryNumber;
  202. $numeralsInWords = $millionsNumeralInWords.' '.$thousandsNumeralsInWords.' '.$lastThreeNumeralsInWords;
  203. }
  204. }
  205. }
  206. if ($count == 6 || $count == 5 || $count == 4) {
  207. $num = $number;
  208. $temporaryNumber = $number;
  209. $thousandsNumeral = floor($num / 1000);
  210. $num = $thousandsNumeral;
  211. $number = $thousandsNumeral;
  212. $thousandsNumeralInWords = smallNumberToText($num, $number, $spelling, $thousands, $femaleSpelling);
  213. $number = $temporaryNumber;
  214. $lastThreeNumerals = $number - ($thousandsNumeral * 1000);
  215. if ($lastThreeNumerals == 0) {
  216. $rouble = getEnding($lastThreeNumerals, $roubles);
  217. $numeralsInWords = $thousandsNumeralInWords.' '.$rouble;
  218. } else {
  219. $temporaryNumber = $number;
  220. $num = $lastThreeNumerals;
  221. $number = $lastThreeNumerals;
  222. $lastThreeNumeralsInWords = smallNumberToText($num, $number, $spelling, $roubles, $menSpelling);
  223. $number = $temporaryNumber;
  224. $numeralsInWords = $thousandsNumeralInWords.' '.$lastThreeNumeralsInWords.' '.$rouble;
  225. }
  226. }
  227. if ($count == 1 || $count == 2 || $count == 3) {
  228. $numeralsInWords = smallNumberToText($num, $number, $spelling, $roubles, $menSpelling);
  229. }
  230. return $numeralsInWords;
  231. }
  232. $amount1 = mt_rand(1,99999999);
  233. $text1 = numberToText($amount1);
  234. echo "На вашем счету \"{$amount1}\" или \"{$text1}\"\n";
  235. $amount2 = mt_rand(1,99999999);
  236. $text2 = numberToText($amount2);
  237. echo "На вашем счету \"{$amount2}\" или \"{$text2}\"\n";
  238. $amount3 = mt_rand(1,99999999);
  239. $text3 = numberToText($amount3);
  240. echo "На вашем счету \"{$amount3}\" или \"{$text3}\"\n";
  241. $amount4 = mt_rand(1,99999999);
  242. $text4 = numberToText($amount4);
  243. echo "На вашем счету \"{$amount4}\" или \"{$text4}\"\n";
  244. ?>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
На вашем счету "19541396" или "девятнадцать миллионов пятьсот сорок одна тысяча триста девяносто шесть рублей"
На вашем счету "95092217" или "девяносто пять миллионов девяносто две тысячи двести семнадцать рублей"
На вашем счету "68912880" или "шестьдесят восемь миллионов девятьсот двенадцать тысяч восемьсот восемьдесят рублей"
На вашем счету "39552631" или "тридцать девять миллионов пятьсот пятьдесят две тысячи шестьсот тридцать один рубль"