fork download
  1. <?php
  2.  
  3. // Staring straight up into the sky ... oh my my
  4. header("Content-Type: text/plain; charset=UTF-8");
  5.  
  6.  
  7. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  8. function inclineWord($number, $word1, $word2, $word5) {
  9. $numLastDigits = $number % 100;
  10. if (($numLastDigits % 10) == 1) {
  11. return $word1;
  12. } elseif (($numLastDigits % 10) > 1 && ($numLastDigits % 10) < 5) {
  13. return $word2;
  14. } else {
  15. return $word5;
  16. }
  17. }
  18.  
  19. /*
  20.   Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  21.   если мы считаем число для мужского рода (один рубль),
  22.   и 1 — для женского (одна тысяча)
  23. */
  24. function smallNumberToText($number, $isFemale) {
  25.  
  26. $spelling = array(
  27. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  28. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  29. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  30. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  31. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  32. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  33. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  34. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  35. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  36. 9 => 'девять', 19 => 'девятнадцать'
  37. );
  38.  
  39. $femaleSpelling = array(
  40. 1 => 'одна', 2 => 'две'
  41. );
  42.  
  43. $number = intval($number);
  44.  
  45. $result = array();
  46.  
  47. if ($number == 0) {
  48. return $result;
  49. }
  50.  
  51. if ($number < 20) {
  52. if ($isFemale && $number <= 2) {
  53. $result[] = $femaleSpelling[$number];
  54. } else {
  55. $result[] = $spelling[$number];
  56. }
  57. } else {
  58. $numberLastDigit = $number % 10;
  59. $numberTens = $number % 100;
  60.  
  61. $numberTensRound = $numberTens - $numberLastDigit;
  62. $numberHunsRound = $number - $numberTens;
  63.  
  64. if ($number >= 20 && $number < 100) {
  65. $result[] = $spelling[$numberTensRound];
  66.  
  67. if ($numberLastDigit != 0) {
  68. if ($isFemale && $numberLastDigit <= 2) {
  69. $result[] = $femaleSpelling[$numberLastDigit];
  70. } else {
  71. $result[] = $spelling[$numberLastDigit];
  72. }
  73. }
  74. } elseif ($number >= 100) {
  75. $result[] = $spelling[$numberHunsRound];
  76. if ($numberTens != 0 ) {
  77. if ($numberTens < 20 || ($numberTens % 10) == 0) {
  78. $result[] = $spelling[$numberTens];
  79. } else {
  80. $result[] = $spelling[$numberTensRound];
  81. if ($isFemale && $numberLastDigit <= 2) {
  82. $result[] = $femaleSpelling[$numberLastDigit];
  83. } else {
  84. $result[] = $spelling[$numberLastDigit];
  85. }
  86. }
  87. }
  88. }
  89. }
  90. $result = implode(" ", $result);
  91. return $result;
  92. }
  93.  
  94. function numberToText($number) {
  95. $number = number_format($number);
  96. $numberSplit = preg_split('/,/', $number);
  97.  
  98. $i = count($numberSplit);
  99.  
  100. $result = array();
  101.  
  102. foreach($numberSplit as $num) {
  103. $fem = 0;
  104.  
  105. if ($i == 3) {
  106. $add = inclineWord($num, "миллион", "миллиона", "миллионов");
  107. } elseif ($i == 2) {
  108. $fem = 1;
  109. $add = inclineWord($num, "тысяча", "тысячи", "тысяч");
  110. } else {
  111. $add = "($number) " . inclineWord($num, "рубль", "рубля", "рублей");
  112. }
  113.  
  114. $i--;
  115.  
  116. $result[] = smallNumberToText($num, $fem) . " $add";
  117.  
  118.  
  119. }
  120. $result = implode(" ", $result);
  121. return $result;
  122. }
  123.  
  124. /* Вызовем функцию несколько раз */
  125. $amount1 = mt_rand(1,99999999);
  126. $text1 = numberToText($amount1);
  127.  
  128. echo "На вашем счету {$text1}\n";
  129.  
  130. $amount2 = mt_rand(1,99999999);
  131. $text2 = numberToText($amount2);
  132.  
  133. echo "На вашем счету {$text2}\n";
  134.  
  135. $amount3 = mt_rand(1,99999999);
  136. $text3 = numberToText($amount3);
  137.  
  138. echo "На вашем счету {$text3}\n";
  139.  
  140. $amount4 = mt_rand(1,99999999);
  141. $text4 = numberToText($amount4);
  142.  
  143. echo "На вашем счету {$text4}\n";
  144.  
  145.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
На вашем счету восемьдесят три миллиона семьсот тридцать две тысячи двадцать пять (83,732,025) рублей
На вашем счету семь миллионов три тысячи девятьсот восемьдесят шесть (7,003,986) рублей
На вашем счету девяносто восемь миллионов двести пятьдесят пять тысяч триста сорок шесть (98,255,346) рублей
На вашем счету тринадцать миллиона девяносто тысяч четыреста девяносто (13,090,490) рублей