fork download
  1. <?php
  2.  
  3. // Staring straight up into the sky ... oh my my
  4.  
  5. function spellSmallNumber($num, $female) {
  6. $result = array();
  7. $spelling = array(
  8. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  9. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  10. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  11. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  12. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  13. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  14. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  15. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  16. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  17. 9 => 'девять', 19 => 'девятнадцать'
  18. );
  19.  
  20. $femaleSpelling = array(
  21. 1 => 'одна', 2 => 'две'
  22. );
  23.  
  24. if ($num == 0) {
  25. return $spelling[$num];
  26. }
  27.  
  28.  
  29. $hundreds = floor($num / 100) * 100; //убирает десятки у сотен
  30. if($hundreds != 0) {
  31. array_push($result, $spelling[$hundreds]);
  32. }
  33. $lastTwo = $num % 100; // оставляет только десятки
  34. if($lastTwo >= 11 and $lastTwo <=19) {
  35. array_push($result, $spelling[$lastTwo]);
  36. } elseif ($lastTwo != 0) {
  37. $units = floor($lastTwo / 10) * 10; //десятки без единиц
  38. if($units != 0) {
  39. array_push($result, $spelling[$units]);
  40. }
  41.  
  42. $hundOne = $lastTwo % 10;
  43.  
  44. if ($female == 1 or $female == 2) {
  45. array_push($result, $femaleSpelling[$hundOne]);
  46. } elseif($female == 0) {
  47. if ($hundOne != 0) {
  48. array_push($result, $spelling[$hundOne]);
  49. }
  50. }
  51.  
  52. }
  53.  
  54.  
  55.  
  56. $text = implode(' ', $result);
  57. return $text;
  58. }
  59.  
  60.  
  61.  
  62. function getWordForm($num, $word, $word1, $word2) {
  63. $x = $num % 100;
  64. if($x >= 11 and $x <=20){
  65. return $word2;
  66. }
  67. else {
  68. $z = $num % 10;
  69. if($z == 1){
  70. return $word; // тысяча, миллион
  71. }
  72. $z = $num % 10;
  73. if($z >= 2 && $z <= 4){
  74. return $word1; //тысячи, миллиона
  75. }
  76. else{
  77. return $word2; // тысяч, миллионов
  78. }
  79. }
  80. }
  81.  
  82. function inclineWord($number, $word1, $word2, $word5) {
  83. $x = $number % 100;
  84. if($x >= 11 and $x <=19){
  85. return $word5;
  86. } else {
  87. $last2Digits = $number % 10;
  88. if($last2Digits == 1) {
  89. return $word1;
  90. } elseif($last2Digits >= 2 and $last2Digits <= 4) {
  91. return $word2;
  92. } else {
  93. return $word5;
  94. }
  95. }
  96. }
  97.  
  98.  
  99. function spellNumber($number) {
  100. $millions = floor($number / 1000000); //миллионы
  101. $thousands = floor($number / 1000);//без тысяч сотни тысяч
  102. $rest = $thousands % 1000; // сотни тысяч
  103. $hundred = $number % 1000; //просто сотни
  104.  
  105. $fem = $rest % 10;
  106. if($fem == 1) {
  107. $female = 1;
  108. } elseif ($fem == 2) {
  109. $female = 2;
  110. } else {
  111. $female = 0;
  112. }
  113.  
  114.  
  115. $result = array();
  116. if($millions !=0){
  117. $result[] = spellSmallNumber($millions, NULL);
  118. $result[] = getWordForm($millions,"миллион","миллиона","миллионов");
  119. }
  120.  
  121. if($rest != 0){
  122. $result[] = spellSmallNumber($rest,$female);
  123. $result[] = getWordForm($rest,"тысяча","тысячи","тысяч");
  124. }
  125.  
  126. $result[] = spellSmallNumber($hundred,NULL);
  127. $result[]= inclineWord($hundred,"рубль","рубля","рублей");
  128.  
  129. $end = implode(" ", $result);
  130. return $end;
  131. }
  132.  
  133. $amount1 = mt_rand(1,99999999);
  134. $text1 = spellNumber($amount1);
  135.  
  136. echo "На вашем счету {$text1}\n";
  137. echo "($amount1)\n";
  138.  
  139. $amount2 = mt_rand(1,99999999);
  140. $text2 = spellNumber($amount2);
  141.  
  142.  
  143. echo "На вашем счету {$text2}\n";
  144. echo "($amount2)\n";
  145.  
  146. $amount3 = mt_rand(1,99999999);
  147. $text3 = spellNumber($amount3);
  148.  
  149.  
  150. echo "На вашем счету {$text3}\n";
  151. echo "($amount3)\n";
  152.  
  153. $amount4 = mt_rand(1,99999999);
  154. $text4 = spellNumber($amount4);
  155.  
  156. echo "На вашем счету {$text4}\n";
  157. echo "($amount4)\n";
  158.  
  159.  
  160.  
  161. ?>
Success #stdin #stdout 0.03s 23768KB
stdin
Standard input is empty
stdout
На вашем счету шестьдесят пять миллионов шестьсот сорок пять тысяч шестьсот тридцать четыре рубля
(65645634)
На вашем счету семьдесят четыре миллиона четыреста шестьдесят шесть тысяч сто сорок восемь рублей
(74466148)
На вашем счету семнадцать миллионов четыреста десять тысяч девятьсот девяносто рублей
(17410990)
На вашем счету девяносто один миллион пятьсот девятнадцать тысяч сто восемьдесят восемь рублей
(91519188)