fork download
  1. <?php
  2.  
  3. // Staring straight up into the sky ... oh my my
  4.  
  5.  
  6. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  7. function inclineWord($number, $word1, $word2, $word5) {
  8.  
  9. $concurrence1 = '/[^123]1$|^1$/';
  10. $concurrence2 = '/([^1][234]|^[234])$/';
  11. $concurrence5 = '/((1\d)|[05-9]|([02-9][05-9]))$/';
  12.  
  13. if (preg_match('/^000$/', $number)) {
  14. } elseif (preg_match($concurrence1, $number)) {
  15. return $word1;
  16. } elseif (preg_match($concurrence2, $number)) {
  17. return $word2;
  18. } elseif (preg_match($concurrence5, $number)) {
  19. return $word5;
  20. }
  21.  
  22. }
  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. if ($isFemale == true) {
  44. $spelling = array_replace($spelling, $femaleSpelling);
  45. }
  46.  
  47. if (preg_match('/^0{1,2}/', $number) && preg_match('/[^0]/', $number)) {
  48. $number = implode('', preg_split("/^(0{1,2})/", $number));
  49. } elseif (preg_match('/000/', $number)) {
  50. $number = '';
  51. return $number;
  52. }
  53.  
  54. if ($number < 10) {
  55. return $spelling[$number];
  56. } else {
  57.  
  58. if ($number > 10) {
  59. $theLastNumber = preg_match("/\d$/", $number, $theLastNumber1);
  60. $theLastNumber2 = $spelling[implode('', $theLastNumber1)];
  61.  
  62. $theSecondNumber = preg_match("/\d\d$/", $number, $theSecondNumber1);
  63. $godDamnIt1 = preg_split("/\d$/", implode('', $theSecondNumber1));
  64.  
  65. if ((implode('', $theSecondNumber1)) > 9) {
  66. array_push($godDamnIt1, "0");
  67. }
  68.  
  69. $theSecondNumber2 = $spelling[implode('', $godDamnIt1)];
  70.  
  71. if ((implode('', $theSecondNumber1)) < 10) {
  72.  
  73. if (preg_match('/^0/', implode('', $theSecondNumber1))) {
  74. $imploded = implode('', preg_split('/^0/', implode('', $theSecondNumber1)));
  75. $theLastNumber2 = $spelling[$imploded];
  76. $theSecondNumber2 = '';
  77. }
  78. } elseif ((implode('', $theSecondNumber1)) > 10 && array_key_exists((implode('', $theSecondNumber1)), $spelling)) {
  79. $theSecondNumber2 = $spelling[(implode('', $theSecondNumber1))];
  80. $theLastNumber2 = '';
  81. }
  82.  
  83. if ($number > 99) {
  84. $godDamnIt2 = preg_split("/\d\d$/", $number);
  85. array_push($godDamnIt2, "0", "0");
  86. $theSFirstNumber2 = $spelling[implode('', $godDamnIt2)];
  87.  
  88. $ifMoreThanHundred = $theSFirstNumber2.' '.$theSecondNumber2.' '.$theLastNumber2;
  89. return $ifMoreThanHundred;
  90. } else {
  91. $ifLessThanHundred = $theSecondNumber2.' '.$theLastNumber2;
  92. return $ifLessThanHundred;
  93. }
  94. }
  95. }
  96. }
  97.  
  98. function numberToText($number) {
  99.  
  100. if ($number <= 999) {
  101. $simpleNumbers = smallNumberToText($number, null).' '.inclineWord($number, ' рубль', ' рубля', ' рублей');
  102. return $simpleNumbers;
  103. } else {
  104. $match = preg_split('//', $number);
  105. $reverse = array_reverse($match);
  106. $leftDifferences = array_diff($reverse, array(''));
  107. $chunking = array_chunk($leftDifferences, 3);
  108. $count = count($chunking);
  109.  
  110. for ($i=0; $i < $count; $i++) {
  111. $reverse1 = array_reverse($chunking[$i]);
  112. }
  113.  
  114. $imploded1 = implode('', array_reverse($chunking[0]));
  115. $imploded2 = implode('', array_reverse($chunking[1]));
  116.  
  117. $isFemale = preg_match("/(1|2)$/", $imploded2);
  118.  
  119. if ($number > 999999) {
  120. $imploded3 = implode('', array_reverse($chunking[2]));
  121. $total2 = smallNumberToText($imploded3, null) .' '. inclineWord($imploded3, ' миллион', ' миллиона', ' миллионов') .' '. smallNumberToText($imploded2, $isFemale) .' '. inclineWord($imploded2, ' тысяча', ' тысячи', ' тысяч') .' '. smallNumberToText($imploded1, null) .' '. inclineWord($number, ' рубль', ' рубля', ' рублей')." ($number)";
  122. return $total2;
  123. } elseif ($number > 999) {
  124. $total1 = smallNumberToText($imploded2, $isFemale) .' '. inclineWord($imploded2, ' тысяча', ' тысячи', ' тысяч') .' '. smallNumberToText($imploded1, null) .' '. inclineWord($number, ' рубль', ' рубля', ' рублей')." ($number)";
  125. return $total1;
  126. }
  127. }
  128. }
  129.  
  130. /* Вызовем функцию несколько раз */
  131. $amount1 = mt_rand(1,99999999);
  132. $text1 = numberToText($amount1);
  133.  
  134. echo "На вашем счету {$text1}\n";
  135.  
  136. $amount2 = mt_rand(1,99999999);
  137. $text2 = numberToText($amount2);
  138.  
  139. echo "На вашем счету {$text2}\n";
  140.  
  141. $amount3 = mt_rand(1,99999999);
  142. $text3 = numberToText($amount3);
  143.  
  144. echo "На вашем счету {$text3}\n";
  145.  
  146. $amount4 = mt_rand(1,99999999);
  147. $text4 = numberToText($amount4);
  148.  
  149. echo "На вашем счету {$text4}\n";
Success #stdin #stdout 0.01s 23940KB
stdin
Standard input is empty
stdout
На вашем счету девяносто один  миллион девятьсот восемьдесят две  тысячи триста шестьдесят четыре  рубля (91982364)
На вашем счету девяносто четыре  миллиона шестьсот тридцать две  тысячи пятьдесят один  рубль (94632051)
На вашем счету девяносто   миллионов пятьсот пятьдесят семь  тысяч триста четырнадцать   рублей (90557314)
На вашем счету шесть  миллионов шестьсот двадцать   тысяч семьсот пятьдесят семь  рублей (6620757)