fork(1) download
  1. <?php
  2.  
  3.  
  4. $number = mt_rand(0,9999999);
  5. $spelling = array(
  6. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  7. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  8. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  9. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  10. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  11. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  12. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  13. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  14. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  15. 9 => 'девять', 19 => 'девятнадцать'
  16. );
  17. $femaleSpelling = array(
  18. 1 => 'одна', 2 => 'две'
  19. );
  20. $menSpelling = array(
  21. 1 => 'один', 2 => 'два'
  22. );
  23. $roubles = array(
  24. 'рубль' => array(1),
  25. 'рубля' => array(2, 3, 4),
  26. 'рублей' => 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)
  27. );
  28. $thousands = array(
  29. 'тысяча' => array(1),
  30. 'тысячи' => array(2, 3, 4),
  31. 'тысяч' => 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)
  32. );
  33. $millions = array(
  34. 'миллион' => array(1),
  35. 'миллиона' => array(2, 3, 4),
  36. 'миллионов' => 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)
  37. );
  38.  
  39. function findMatch($num, $spelling) {
  40. $result = 0;
  41. foreach ($spelling as $numbers => $numbersInWords) {
  42. if ($num == $numbers) {
  43. $result = $numbersInWords;
  44. return $result;
  45. }
  46. }
  47. }
  48.  
  49. function getRoubles($num, $roubles) {
  50. foreach ($roubles as $forms => $numerals) {
  51. foreach ($numerals as $array => $numeral) {
  52. if ($num == $numeral) {
  53. return $forms;
  54. }
  55. }
  56. }
  57. }
  58. function threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $femaleSpelling) {
  59. foreach ($spelling as $numbers => $numbersInWords) {
  60. if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
  61. $result = $numbersInWords;
  62. $rouble = getRoubles($num, $roubles);
  63. $i++;
  64. }
  65. }
  66. if ($i > 0) {
  67. $numeralsInWords = $result.' '.$rouble;
  68.  
  69. } else {
  70. $firstNumeral = floor($num / 100); //находим первое число в сотне
  71. $hundred = $firstNumeral * 100;
  72. $num = $hundred;
  73. $hundredInWords = findMatch($num, $spelling);
  74. $ten = $number - $hundred; // Получаем десятки
  75. $num = $ten;
  76. if ($num == 0) {
  77. } else {
  78. foreach ($spelling as $numbers => $numbersInWords) {
  79. if ($num == $numbers) {
  80. $result = $numbersInWords;
  81. if ($num == 1) {
  82. $result = findMatch($num, $femaleSpelling);
  83. }
  84. if ($num == 2) {
  85. $result = findMatch($num, $femaleSpelling);
  86. }
  87. $rouble = getRoubles($num, $roubles);
  88. $x++;
  89. }
  90. }
  91. }
  92. if ($x > 0) {
  93. $numeralsInWords = $hundredInWords.' '.$result.' '.$rouble;
  94. } else {
  95. $tenWithoutOne = floor(($number - $hundred) / 10) * 10;
  96. $num = $tenWithoutOne;
  97. $tenWithoutOneInWords = findMatch($num, $spelling);
  98. $one = $number - ($hundred + $tenWithoutOne);
  99. $num = $one;
  100. $oneInWords = findMatch($num, $spelling);
  101. if ($num == 1) {
  102. $oneInWords = findMatch($num, $femaleSpelling);
  103. }
  104. if ($num == 2) {
  105. $oneInWords = findMatch($num, $femaleSpelling);
  106. }
  107. $rouble = getRoubles($num, $roubles);
  108. $numeralsInWords = $hundredInWords.' '.$tenWithoutOneInWords.' '.$oneInWords.' '.$rouble;
  109. }
  110. }
  111. return $numeralsInWords;
  112. }
  113. function twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $femaleSpelling) {
  114. foreach ($spelling as $numbers => $numbersInWords) {
  115. if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
  116. $result = $numbersInWords;
  117. if ($num == 1) {
  118. $result = findMatch($num, $femaleSpelling);
  119. }
  120. if ($num == 2) {
  121. $result = findMatch($num, $femaleSpelling);
  122. }
  123. $rouble = getRoubles($num, $roubles);
  124. $i++;
  125. }
  126. }
  127. if ($i > 0) {
  128. $numeralsInWords = $result.' '.$rouble;
  129. } else {
  130. $firstNumeral = floor($num / 10); //находим первое число в десятке
  131. $ten = $firstNumeral * 10;
  132. $num = $ten;
  133. $tenInWords = findMatch($num, $spelling);
  134. $one = $number - $ten;
  135. $num = $one;
  136. $oneInWords = findMatch($num, $spelling);
  137. $result = $oneInWords;
  138. if ($num == 1) {
  139. $oneInWords = findMatch($num, $femaleSpelling);
  140. }
  141. if ($num == 2) {
  142. $oneInWords = findMatch($num, $femaleSpelling);
  143. }
  144. $rouble = getRoubles($num, $roubles);
  145. $numeralsInWords = $tenInWords.' '.$oneInWords.' '.$rouble;
  146. }
  147. return $numeralsInWords;
  148. }
  149. function oneNumeralToText($num, $number, $spelling, $roubles, $femaleSpelling) {
  150. foreach ($spelling as $numbers => $numbersInWords) {
  151. if ($num == $numbers) { // Если число соответствует иммеющимс в массиве $spelling - выводим их и форму рублей.
  152. $result = $numbersInWords;
  153. $rouble = getRoubles($num, $roubles);
  154. if ($num == 1) {
  155. $result = findMatch($num, $femaleSpelling);
  156. }
  157. if ($num == 2) {
  158. $result = findMatch($num, $femaleSpelling);
  159. }
  160. }
  161. }
  162. $numeralsInWords = $result.' '.$rouble;
  163. return $numeralsInWords;
  164. }
  165. $text = strval($number);
  166. $count = mb_strlen($text);
  167. $num = $number;
  168. $i = 0;
  169. $x = 0;
  170. if ($count == 7) {
  171. $firstNumeral = floor($num / 1000000);
  172. $num = $firstNumeral;
  173. $firstNumeralInWords = findMatch($num, $spelling);
  174. $million = getRoubles($num, $millions);
  175. /*Ищет ноль в начале следующей тройки чисел*/
  176. $nextNumeral = floor(($number - ($firstNumeral * 1000000)) / 1000);
  177. if ($nextNumeral == 0) {
  178. $million = getRoubles($firstNumeral, $millions);
  179. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$rouble;
  180. } else {
  181. $text = strval($nextNumeral);
  182. $threeNumerals = mb_strlen($text);
  183. $temporary = $number;
  184. $num = $nextNumeral;
  185. $number = $nextNumeral;
  186. if ($threeNumerals == 3) {
  187. $threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $femaleSpelling);
  188. $number = $temporary;
  189. $general = $threeNumeralsInWords;
  190. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$threeNumeralsInWords;
  191. }
  192. if ($threeNumerals == 2) {
  193. $twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $menSpelling);
  194. $number = $temporary;
  195. $general = $twoNumeralsInWords;
  196. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$twoNumeralsInWords;
  197. }
  198. if ($threeNumerals == 1) {
  199. $oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $thousands, $i, $x, $menSpelling);
  200. $number = $temporary;
  201. $general = $oneNumeralInWords;
  202. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$oneNumeralInWords;
  203. }
  204. }
  205. $threeLastNumerals = $number - (($firstNumeral * 1000000) + ($nextNumeral * 1000));
  206. if ($threeLastNumerals == 0) {
  207. $rouble = getRoubles($nextNumeral, $roubles);
  208. $number = $temporary;
  209. if ($threeLastNumerals != $nextNumeral) {
  210. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$rouble;
  211. }
  212. if ($threeLastNumerals == $nextNumeral) {
  213. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$rouble;
  214. }
  215.  
  216. } else {
  217. $text = strval($threeLastNumerals);
  218. $threeNumerals = mb_strlen($text);
  219. $num = $threeLastNumerals;
  220. $temporary = $number;
  221. $number = $threeLastNumerals;
  222. if ($threeNumerals == 3) {
  223. $threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  224. $number = $temporary;
  225. if ($nextNumeral != 0) {
  226. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$threeNumeralsInWords;
  227. }
  228. if ($nextNumeral == 0) {
  229. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$threeNumeralsInWords;
  230. }
  231. }
  232. if ($threeNumerals == 2) {
  233. $twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  234. $number = $temporary;
  235. if ($nextNumeral != 0) {
  236. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$twoNumeralsInWords;
  237. }
  238. if ($nextNumeral == 0) {
  239. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$twoNumeralsInWords;
  240. }
  241. }
  242. if ($threeNumerals == 1) {
  243. $oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
  244. if ($nextNumeral != 0) {
  245. $number = $temporary;
  246. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$general.' '.$oneNumeralInWords;
  247. }
  248. if ($nextNumeral == 0) {
  249. $number = $temporary;
  250. $numeralsInWords = $firstNumeralInWords.' '.$million.' '.$oneNumeralInWords;
  251. }
  252. }
  253. }
  254. }
  255. if ($count == 6) {
  256. $threeNumerals = floor($num / 1000);
  257. $num = $threeNumerals;
  258. $temporary = $number;
  259. $number = $threeNumerals;
  260. $threeNumeralsToText = threeNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $femaleSpelling);
  261. /*Ищет ноль в начале следующей тройки чисел*/
  262. $nextNumeral = $temporary - ($threeNumerals * 1000);
  263. if ($nextNumeral == 0) {
  264. $rouble = getRoubles($nextNumeral, $roubles);
  265. $number = $temporary;
  266. $numeralsInWords = $threeNumeralsToText.' '.$rouble;
  267. } else {
  268. $text = strval($nextNumeral);
  269. $threeNumerals = mb_strlen($text);
  270. $num = $nextNumeral;
  271. $number = $nextNumeral;
  272. if ($threeNumerals == 3) {
  273. $threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  274. $number = $temporary;
  275. $numeralsInWords = $threeNumeralsToText.' '.$threeNumeralsInWords;
  276. }
  277. if ($threeNumerals == 2) {
  278. $twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  279. $number = $temporary;
  280. $numeralsInWords = $threeNumeralsToText.' '.$twoNumeralsInWords;
  281. }
  282. if ($threeNumerals == 1) {
  283. $oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
  284. $number = $temporary;
  285. $numeralsInWords = $threeNumeralsToText.' '.$oneNumeralInWords;
  286. }
  287. }
  288. }
  289. if ($count == 5) {
  290. $twoNumerals = floor($num / 1000);
  291. $num = $twoNumerals;
  292. $temporary = $number;
  293. $number = $twoNumerals;
  294. $thousandsInWords = twoNumeralsToText($num, $number, $spelling, $thousands, $i, $x, $femaleSpelling);
  295. /*Ищет ноль в начале следующей тройки чисел*/
  296. $nextNumeral = $temporary - ($twoNumerals * 1000);
  297. if ($nextNumeral == 0) {
  298. $rouble = getRoubles($nextNumeral, $roubles);
  299. $number = $temporary;
  300. $numeralsInWords = $thousandsInWords.' '.$rouble;
  301. } else {
  302. $text = strval($nextNumeral);
  303. $threeNumerals = mb_strlen($text);
  304. $num = $nextNumeral;
  305. $number = $nextNumeral;
  306. if ($threeNumerals == 3) {
  307. $threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x);
  308. $number = $temporary;
  309. $numeralsInWords = $thousandsInWords.' '.$threeNumeralsInWords;
  310. }
  311. if ($threeNumerals == 2) {
  312. $twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  313. $number = $temporary;
  314. $numeralsInWords = $thousandsInWords.' '.$twoNumeralsInWords;
  315. }
  316. if ($threeNumerals == 1) {
  317. $oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
  318. $number = $temporary;
  319. $numeralsInWords = $thousandsInWords.' '.$oneNumeralInWords;
  320. }
  321. }
  322. }
  323. if ($count == 4) {
  324. $firstNumeral = floor($num / 1000);
  325. $num = $firstNumeral;
  326. $firstNumeralInWords = findMatch($num, $spelling);
  327. if ($num == 1) {
  328. $firstNumeralInWords = findMatch($num, $femaleSpelling);
  329. }
  330. if ($num == 2) {
  331. $firstNumeralInWords = findMatch($num, $femaleSpelling);
  332. }
  333. $thousand = getRoubles($num, $thousands);
  334. /*Ищет ноль в начале следующей тройки чисел*/
  335. $nextNumeral = $number - ($firstNumeral * 1000);
  336. if ($nextNumeral == 0) {
  337. $rouble = getRoubles($nextNumeral, $roubles);
  338. $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$rouble;
  339. } else {
  340. $text = strval($nextNumeral);
  341. $threeNumerals = mb_strlen($text);
  342. $temporary = $number;
  343. $num = $nextNumeral;
  344. $number = $nextNumeral;
  345. if ($threeNumerals == 3) {
  346. $threeNumeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  347. $number = $temporary;
  348. $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$threeNumeralsInWords;
  349. }
  350. if ($threeNumerals == 2) {
  351. $twoNumeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  352. $number = $temporary;
  353. $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$twoNumeralsInWords;
  354. }
  355. if ($threeNumerals == 1) {
  356. $oneNumeralInWords = oneNumeralToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  357. $number = $temporary;
  358. $numeralsInWords = $firstNumeralInWords.' '.$thousand.' '.$oneNumeralInWords;
  359. }
  360. }
  361. }
  362. if ($count == 3) {
  363. $numeralsInWords = threeNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  364.  
  365. }
  366. if ($count == 2) {
  367. $numeralsInWords = twoNumeralsToText($num, $number, $spelling, $roubles, $i, $x, $menSpelling);
  368. }
  369. if ($count == 1) {
  370. $numeralsInWords = oneNumeralToText($num, $number, $spelling, $roubles, $menSpelling);
  371. }
  372.  
  373. echo "На счету \"$number руб.\" или \"$numeralsInWords\".";
Success #stdin #stdout 0.05s 52480KB
stdin
Standard input is empty
stdout
На счету "8103619 руб." или "восемь миллионов сто три тысячи шестьсот девятнадцать рублей".