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