fork download
  1. <?php
  2. // Staring straight up into the sky ... oh my my
  3.  
  4. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  5. function inclineWord($number) {
  6. $word5 = "";
  7. $lastDigits = $number % 100;
  8. $lastDigit = $lastDigits % 10;
  9. if ($lastDigit == 1 && $lastDigits != 11) {
  10. $word5 = " рубль";
  11. }
  12. if ($lastDigit > 1 && $lastDigit < 5) {
  13. $word5 = " рубля";
  14. }
  15. if ($lastDigit == 0 || ($lastDigits > 10 && $lastDigits < 20) || ($lastDigit > 4 && $lastDigit < 10)) {
  16. $word5 = " рублей";
  17. }
  18. return $word5;
  19. }
  20. /*
  21.  * Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  22.  * если мы считаем число для мужского рода (один рубль),
  23.  * и 1 — для женского (одна тысяча)
  24.  */
  25. function smallNumberToText($number, $isFemale) {
  26. $spelling = array (
  27. 0 => '',
  28. 10 => 'десять',
  29. 100 => 'сто',
  30. 1 => 'один',
  31. 11 => 'одиннадцать',
  32. 20 => 'двадцать',
  33. 200 => 'двести',
  34. 2 => 'два',
  35. 12 => 'двенадцать',
  36. 30 => 'тридцать',
  37. 300 => 'триста',
  38. 3 => 'три',
  39. 13 => 'тринадцать',
  40. 40 => 'сорок',
  41. 400 => 'четыреста',
  42. 4 => 'четыре',
  43. 14 => 'четырнадцать',
  44. 50 => 'пятьдесят',
  45. 500 => 'пятьсот',
  46. 5 => 'пять',
  47. 15 => 'пятнадцать',
  48. 60 => 'шестьдесят',
  49. 600 => 'шестьсот',
  50. 6 => 'шесть',
  51. 16 => 'шестнадцать',
  52. 70 => 'семьдесят',
  53. 700 => 'семьсот',
  54. 7 => 'семь',
  55. 17 => 'семнадцать',
  56. 80 => 'восемьдесят',
  57. 800 => 'восемьсот',
  58. 8 => 'восемь',
  59. 18 => 'восемнадцать',
  60. 90 => 'девяносто',
  61. 900 => 'девятьсот',
  62. 9 => 'девять',
  63. 19 => 'девятнадцать'
  64. );
  65. $femaleSpelling = array (
  66. 'одна ',
  67. 'две '
  68. );
  69. $arrNumber = [ ];
  70. $word = "";
  71. $arrNumber = preg_split ( "//", $number, - 1, PREG_SPLIT_NO_EMPTY );
  72. $numberCount = count ( $arrNumber );
  73. if ($numberCount == 1 && $isFemale == 0) { // обрабатываем 1-9
  74. $word = $spelling [$arrNumber [0]];
  75. } else if ($numberCount == 1 && $isFemale == 1) {
  76. if ($arrNumber [0] == 1) {
  77. $word = $femaleSpelling [0];
  78. }
  79. }
  80. if ($numberCount == 2 && $isFemale == 0) { // обрабатываем 10-99
  81. if ($number > 10 && $number < 20) {
  82. $word = $spelling [$number];
  83. } else if ($arrNumber [1] == 1 && $isFemale == 0) {
  84. $word = $spelling [($arrNumber [0] * 10)] . " " . $spelling [$arrNumber [1]];
  85. } else
  86. $word = $spelling [($arrNumber [0] * 10)] . " " . $spelling [$arrNumber [1]];
  87. } else if ($numberCount == 2 && $isFemale == 1) {
  88. if ($arrNumber [1] == 1) {
  89. $word = $spelling [($arrNumber [0] * 10)] . " " . $femaleSpelling [0];
  90. } else
  91. $word = $spelling [($arrNumber [0] * 10)] . " " . $femaleSpelling [1];
  92. }
  93. if ($numberCount == 3 && $isFemale == 0) { // обрабатываем 100-999
  94. $last = $arrNumber [1] . $arrNumber [2];
  95. if ($last > 10 && $last < 20) {
  96. $word = $spelling [($arrNumber [0] * 100)] . " " . $spelling [$last];
  97. } else
  98. $word = $spelling [($arrNumber [0] * 100)] . " " . $spelling [$arrNumber [1] * 10] . " " . $spelling [$arrNumber [2]];
  99. } else if ($numberCount == 3 && $isFemale == 1) {
  100. $last = $arrNumber [1] . $arrNumber [2];
  101. if ($arrNumber [2] == 1) {
  102. $word = $spelling [($arrNumber [0] * 100)] . " " . $spelling [($arrNumber [1] * 10)] . " " . $femaleSpelling [0];
  103. } else
  104. $word = $word = $spelling [($arrNumber [0] * 100)] . " " . $spelling [($arrNumber [1] * 10)] . " " . $femaleSpelling [1];
  105. }
  106. return $word;
  107. }
  108. function numberToText($number) {
  109. if ($number == 0) {
  110. return "ноль (0) рублей";
  111. }
  112.  
  113. $thousandSpelling = [
  114. " тысяча ",
  115. " тысячи ",
  116. " тысяч "
  117. ];
  118. $millionSpelling = [
  119. " миллион ",
  120. " миллиона ",
  121. " миллионов "
  122. ];
  123. $numberArray = preg_split ( "//", $number, - 1, PREG_SPLIT_NO_EMPTY );
  124. $numberArrayCount = count ( $numberArray );
  125. $text = "";
  126. if ($number < 1000) {
  127. $text = smallNumberToText ( $number, 0 ) . " ($number)" . inclineWord ( $number );
  128. }
  129. if ($numberArrayCount == 4) { // тысячи
  130. $last = $numberArray [1] . $numberArray [2] . $numberArray [3];
  131. if ($numberArray [0] == 1) {
  132. $text = smallNumberToText ( $numberArray [0], 1 ) . $thousandSpelling [0] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  133. } else if ($numberArray [0] > 1 && $numberArray [0] < 5) {
  134. $text = smallNumberToText ( $numberArray [0], 1 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  135. } else
  136. $text = smallNumberToText ( $numberArray [0], 1 ) . $thousandSpelling [2] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  137. }
  138. if ($numberArrayCount == 5) { // десятки тысяч
  139. $first = $numberArray [0] . $numberArray [1];
  140. $last = $numberArray [2] . $numberArray [3] . $numberArray [4];
  141. if ($numberArray [1] == 1) {
  142. $text = smallNumberToText ( $first, 1 ) . $thousandSpelling [0] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  143. } else if ($numberArray [1] == 2) {
  144. $text = smallNumberToText ( $first, 1 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  145. } else if ($numberArray [1] > 2 && $numberArray [1] < 5) {
  146. $text = smallNumberToText ( $first, 0 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  147. } else
  148. $text = smallNumberToText ( $first, 0 ) . $thousandSpelling [2] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  149. }
  150. if ($numberArrayCount == 6) { // сотни тысяч
  151. $first = $numberArray [0] . $numberArray [1] . $numberArray [2];
  152. $last = $numberArray [3] . $numberArray [4] . $numberArray [5];
  153. if ($numberArray [2] == 1) {
  154. $text = smallNumberToText ( $first, 1 ) . $thousandSpelling [0] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  155. } else if ($numberArray [2] == 2) {
  156. $text = smallNumberToText ( $first, 1 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  157. } else if ($numberArray [2] > 2 && $numberArray [2] < 5) {
  158. $text = smallNumberToText ( $first, 0 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  159. } else
  160. $text = smallNumberToText ( $first, 0 ) . $thousandSpelling [2] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  161. }
  162. if ($numberArrayCount == 7) { // миллионы
  163. $first = $numberArray [0];
  164. $middle = $numberArray [1] . $numberArray [2] . $numberArray [3];
  165. $last = $numberArray [4] . $numberArray [5] . $numberArray [6];
  166. if ($numberArray [3] == 1) {
  167. $text = smallNumberToText ( $middle, 1 ) . $thousandSpelling [0] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  168. } else if ($numberArray [3] == 2) {
  169. $text = smallNumberToText ( $middle, 1 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  170. } else if ($numberArray [3] > 2 && $numberArray [3] < 5) {
  171. $text = smallNumberToText ( $middle, 0 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  172. } else
  173. $text = smallNumberToText ( $middle, 0 ) . $thousandSpelling [2] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  174. if ($numberArray [0] == 1) {
  175. $text = smallNumberToText ( $first, 0 ) . " " . $millionSpelling [0] . $text;
  176. } else if ($numberArray [0] > 1 && $numberArray [0] < 5) {
  177. $text = smallNumberToText ( $first, 0 ) . " " . $millionSpelling [1] . $text;
  178. } else
  179. $text = smallNumberToText ( $first, 0 ) . " " . $millionSpelling [2] . $text;
  180. }
  181. if ($numberArrayCount == 8) { // десятки миллионов
  182. $first = $numberArray [0] . $numberArray [1];
  183. $middle = $numberArray [2] . $numberArray [3] . $numberArray [4];
  184. $last = $numberArray [5] . $numberArray [6] . $numberArray [7];
  185. if ($numberArray [4] == 1) {
  186. $text = smallNumberToText ( $middle, 1 ) . $thousandSpelling [0] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  187. } else if ($numberArray [4] == 2) {
  188. $text = smallNumberToText ( $middle, 1 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  189. } else if ($numberArray [4] > 2 && $numberArray [4] < 5) {
  190. $text = smallNumberToText ( $middle, 0 ) . $thousandSpelling [1] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  191. } else
  192. $text = smallNumberToText ( $middle, 0 ) . $thousandSpelling [2] . smallNumberToText ( $last, 0 ) . " ($number)" . inclineWord ( $last );
  193. if ($numberArray [1] == 1) {
  194. $text = smallNumberToText ( $first, 0 ) . " " . $millionSpelling [0] . $text;
  195. } else if ($numberArray [1] > 1 && $numberArray [1] < 5) {
  196. $text = smallNumberToText ( $first, 0 ) . " " . $millionSpelling [1] . $text;
  197. } else
  198. $text = smallNumberToText ( $first, 0 ) . " " . $millionSpelling [2] . $text;
  199. }
  200. $text = preg_replace ( "/ +/", " ", $text );
  201. return $text;
  202. }
  203.  
  204. /* Вызовем функцию несколько раз */
  205.  
  206. $amount1 = mt_rand ( 1, 99999999 );
  207. $text1 = numberToText ( $amount1 );
  208.  
  209. echo "На вашем счету {$text1}\n";
  210.  
  211. $amount2 = mt_rand ( 1, 99999999 );
  212. $text2 = numberToText ( $amount2 );
  213.  
  214. echo "На вашем счету {$text2}\n";
  215.  
  216. $amount3 = mt_rand ( 1, 99999999 );
  217. $text3 = numberToText ( $amount3 );
  218.  
  219. echo "На вашем счету {$text3}\n";
  220.  
  221. $amount4 = mt_rand ( 1, 99999999 );
  222. $text4 = numberToText ( $amount4 );
  223.  
  224. echo "На вашем счету {$text4}\n";
  225.  
Success #stdin #stdout 0.02s 24144KB
stdin
Standard input is empty
stdout
На вашем счету семьдесят семь миллионов триста тридцать одна тысяча триста шестьдесят три (77331363) рубля
На вашем счету двенадцать миллиона девятьсот шестьдесят одна тысяча шестьсот пятьдесят девять (12961659) рублей
На вашем счету пять миллионов двести восемьдесят тысяч семьсот тридцать три (5280733) рубля
На вашем счету тридцать пять миллионов семьдесят две тысячи сто семьдесят (35072170) рублей