fork download
  1. <?php
  2.  
  3.  
  4. //$number = 301;
  5. $spelling = array(
  6. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  7. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  8. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  9. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  10. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  11. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  12. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  13. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  14. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  15. 9 => 'девять', 19 => 'девятнадцать'
  16. );
  17. $roubles = array(
  18. 'рубль' => array(1),
  19. 'рубля' => array(2, 3, 4),
  20. 'рублей' => array(0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900)
  21. );
  22. function findMatches($num, $spelling) {
  23. $result = 0;
  24. foreach ($spelling as $keys => $values) {
  25. if ($num == $keys) {
  26. $result = $values;
  27. break;
  28. }
  29. } return $result;
  30. }
  31. function findRoublesForm($num, $roubles) {
  32. $rouble = 0;
  33. foreach ($roubles as $form => $numerals) {
  34. foreach ($numerals as $s => $numeral) {
  35. if ($num == $numeral) {
  36. $rouble = $form;
  37. }
  38. }
  39. } return $rouble;
  40. }
  41. function smallNumberToText($number, $spelling, $roubles) {
  42. $i = 0;
  43. $o = 0;
  44. $text = strval($number);
  45. $countLetters = mb_strlen($text);
  46. foreach ($spelling as $keys => $values) {
  47. if ($number == $keys) {
  48. $splash = $values;
  49. $o++;
  50. break;
  51. }
  52. }
  53. $num = $number;
  54. $rouble = findRoublesForm($num, $roubles);
  55. if ($o > 0) {
  56. $numberInWords = $splash.' '.$rouble;
  57. } else {
  58. if ($countLetters == 3) {
  59. $firstNumberOfTheHundred = mb_substr($text, 0, 1);
  60. $findHundredEqual = (int)$firstNumberOfTheHundred * 100;
  61. if ($findHundredEqual == 0) {
  62. } else {
  63. $num = $findHundredEqual;
  64. $hundred = findMatches($num, $spelling);
  65. $rouble = findRoublesForm($num, $roubles);
  66. }
  67. $findLastTwoNumerals = mb_substr($text, 1, 2);
  68. $lastTwoNumerals = (int)$findLastTwoNumerals;
  69. foreach ($spelling as $keys => $values) {
  70. if ($lastTwoNumerals == $keys) {
  71. $ten = $values;
  72. $i++;
  73. break;
  74. }
  75. }
  76. $num = $lastTwoNumerals;
  77. $rouble = findRoublesForm($num, $roubles);
  78. if ($i > 0) {
  79. $numberInWords = $hundred.' '.$ten.' '.$rouble;
  80. } else {
  81. $firstNumberOfTheTen = mb_substr($text, 1, 1);
  82. $findTenEqual = (int)$firstNumberOfTheTen * 10;
  83. if ($findTenEqual == 0) {
  84. } else {
  85. $num = $findTenEqual;
  86. $ten = findMatches($num, $spelling);
  87.  
  88. }
  89. $lastOneNumeral = mb_substr($text, 2, 1);
  90. $findLastOneNumeral = (int)$lastOneNumeral;
  91. $num = $findLastOneNumeral;
  92. $one = findMatches($num, $spelling);
  93. $rouble = findRoublesForm($num, $roubles);
  94. $numberInWords = $hundred.' '.$ten.' '.$one.' '.$rouble;
  95. }
  96. }
  97.  
  98. if ($countLetters == 2) {
  99. $i = 0;
  100. $lastTwoNumerals = mb_substr($text, 0, 2);
  101. $twoNumerals = (int)$lastTwoNumerals;
  102. foreach ($spelling as $keys => $values) {
  103. if ($twoNumerals == $keys) {
  104. $ten = $values;
  105. $i++;
  106. break;
  107. }
  108. }
  109. $num = $twoNumerals;
  110. $rouble = findRoublesForm($num, $roubles);
  111. if ($i > 0) {
  112. $numberInWords = $hundred.' '.$ten.' '.$rouble;
  113. } else {
  114. $firstNumeralOfTheTen = mb_substr($text, 0, 1);
  115. $tenEqual = (int)$firstNumeralOfTheTen * 10;
  116. if ($tenEqual == 0) {
  117. } else {
  118. $num = $tenEqual;
  119. $ten = findMatches($num, $spelling);
  120. }
  121. $lastNumeral = mb_substr($text, 1, 1);
  122. $findLastNumeral = (int)$lastNumeral;
  123. $num = $findLastNumeral;
  124. $one = findMatches($num, $spelling);
  125. $rouble = findRoublesForm($num, $roubles);
  126. $numberInWords = $ten.' '.$one.' '.$rouble;
  127. }
  128. }
  129. }
  130. return $numberInWords;
  131. }
  132.  
  133. $number = mt_rand(1,999);
  134. $cheking = smallNumberToText($number, $spelling, $roubles);
  135. echo "\"{$number}\" - это \"{$cheking}\".";
  136.  
  137. ?>
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
"729" - это "семьсот двадцать девять рублей".