fork download
  1. <?php
  2. // Staring straight up into the sky ... oh my my
  3.  
  4. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  5. function inclineWord($number, $word1, $word2, $word5) {
  6. /* DIY */
  7. if ($number == 1) {
  8. return "рубль";
  9. }elseif ($number == 2 || $number == 3 || $number == 4){
  10. return "рубля";
  11. }elseif ($number == 0 || $number >5 and $number<21){
  12. return "рублей";
  13. }else {
  14. return "рублей";
  15. }
  16. }
  17.  
  18. /*
  19.   Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  20.   если мы считаем число для мужского рода (один рубль),
  21.   и 1 — для женского (одна тысяча)
  22. */
  23. function smallNumberToText($number, $isFemale)
  24. {
  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. /* DIY */
  43. $splitter = preg_split('//u', $number, -1, PREG_SPLIT_NO_EMPTY);
  44.  
  45. if (count($splitter) <3 and $number <= 20) {
  46. return $spelling[$number];
  47. } elseif ($number > 20 and $number < 100) {
  48. $decimal = $splitter[0] * 10;
  49. return ($spelling[$decimal] . " " . $spelling[$splitter[1]]);
  50.  
  51. } elseif ($number >= 100 and $number < 1000) {
  52. // Тройка чисел!
  53. if (count($splitter) ==3 and $splitter[2]==0){
  54. $hundredth = $splitter[0] * 100;
  55. $decimal = $splitter[1] * 10;
  56. return ($spelling[$hundredth] . " " . $spelling[$decimal]);
  57. }elseif(count($splitter) ==3 and $splitter[1]==0){
  58. $hundredth = $splitter[0] * 100;
  59. return ($spelling[$hundredth] . " " . $spelling[$splitter[2]]);
  60. } elseif(count($splitter) ==3 and $splitter[0]==0 and $splitter[1]==0){
  61. $hundredth = $splitter[0] * 100;
  62. return ($spelling[$hundredth]);
  63. } elseif(count($splitter) ==3 and $splitter[0]==0){
  64. $decimal = $splitter[1] * 10;
  65. return ($spelling[$decimal] . " " . $spelling[$splitter[2]]);
  66. } elseif(count($splitter) ==3 and $splitter[0]==0 and $splitter[1]==0){
  67. return ($spelling[$splitter[2]]);
  68. }else{
  69. $hundredth = $splitter[0] * 100;
  70. $decimal = $splitter[1] * 10;
  71. return ($spelling[$hundredth] . " " . $spelling[$decimal] . " " . $spelling[$splitter[2]]);
  72. }
  73. } elseif ($number > 1000 and $number < 2000) {
  74. echo "Принимаю больше тысячи - " . $number;
  75. $numbers = preg_split('//u', $number, -1, PREG_SPLIT_NO_EMPTY);
  76. if (($numbers[2]) == 0) {
  77. $thousands = $numbers[0] * 1000;
  78. $hundredth = $numbers[0] * 100;
  79. return ($spelling[$thousands] . " " . $spelling[$hundredth] . " " . $spelling[$numbers[2]]);
  80.  
  81. } elseif (($numbers[1]) == 0 and ($numbers[2]) == 0) {
  82. $thousands = $numbers[0] * 1000;
  83. return ($spelling[$thousands] . " " . $spelling[$numbers[2]]);
  84.  
  85. } elseif (($numbers[1]) == 0) {
  86. $thousands = $numbers[0] * 1000;
  87. $decimal = $numbers[2] * 10;
  88. return ($spelling[$thousands] . " " . ($spelling[$decimal]) . " " . $spelling[$numbers[2]]);
  89. } else {
  90. $thousands = $numbers[0] * 1000;
  91. $hundredth = $numbers[1] * 100;
  92. $decimal = $numbers[2] * 10;
  93. return ($femaleSpelling[1] . " " . $thousands[1] . " " . $spelling[$hundredth] . " " . $spelling[$decimal] . " " . $spelling[$numbers[3]]);
  94. }
  95. }
  96.  
  97. else {
  98. }
  99. }
  100.  
  101. function numberToText($number) {
  102.  
  103. /* DIY */
  104. $resultString="";
  105. $lastTreeDigit="";
  106. echo "Исходное число ".$number."\n";
  107.  
  108. $separatedNumbers= preg_split('//u', $number, NULL , PREG_SPLIT_NO_EMPTY);
  109. $j=count($separatedNumbers);
  110. $lastDigit=array_pop($separatedNumbers);
  111.  
  112. // один
  113. $spedec = array(
  114. 0 => 'Один',
  115. 1 => 'десять',
  116. 2 => 'сто',
  117. 3 => 'тысяча',
  118. 4 => 'миллион',
  119. 5 => 'миллиард',
  120. 6 => 'триллион',
  121. 7 => 'квадриллион',
  122. 8 => 'квинтиллион',
  123. 9 => 'секстиллион',
  124. 10 => 'септиллион',
  125. 11 => 'октиллион',
  126. 12 => 'нониллион',
  127. 13 => 'дециллион'
  128. );
  129.  
  130. //пять
  131. $spedecSecond = array(
  132. 1 => 'десять',
  133. 2 => 'сто',
  134. 3 => 'тысячь',
  135. 4 => 'миллионов',
  136. 5 => 'миллиардов',
  137. 6 => 'триллионов',
  138. 7 => 'квадриллионов',
  139. 8 => 'квинтиллионов',
  140. 9 => 'секстиллионов',
  141. 10 => 'септиллионов',
  142. 11 => 'октиллионов',
  143. 12 => 'нониллионов',
  144. 13 => 'дециллионов'
  145. );
  146.  
  147. // три
  148. $spedecThird = array(
  149. 1 => 'десять',
  150. 2 => 'сто',
  151. 3 => 'тысячи',
  152. 4 => 'миллиона',
  153. 5 => 'миллиарда',
  154. 6 => 'триллиона',
  155. 7 => 'квадриллиона',
  156. 8 => 'квинтиллиона',
  157. 9 => 'секстиллиона',
  158. 10 => 'септиллиона',
  159. 11 => 'октиллиона',
  160. 12 => 'нониллиона',
  161. 13 => 'дециллиона'
  162. );
  163.  
  164. for ($i=0; $i <count($separatedNumbers); $i++) {
  165. $j--;
  166. if ($j>2){
  167. switch (($separatedNumbers[$i])) {
  168. case 1;
  169. $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedec[$j];
  170. break;
  171. case 2;
  172. case 3;
  173. case 4;
  174. $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedecThird[$j];
  175. break;
  176. case 5;
  177. case 6;
  178. case 7;
  179. case 8;
  180. case 9;
  181. case 10:
  182. $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedecSecond[$j];
  183. break;
  184. default;
  185. $resultString = $resultString ." ". smallNumberToText(($separatedNumbers[$i]), 0)." ".$spedec[$j];
  186. break;
  187. }
  188. }else{
  189. $lastTreeDigit = $lastTreeDigit . $separatedNumbers[$i];
  190. }
  191. }
  192. $resultString = $resultString . " ". smallNumberToText(($lastTreeDigit. $lastDigit), 0). " ". inclineWord($lastDigit, 1, 1, 1) . " ";;
  193. $result=$resultString;
  194. return $result;
  195. }
  196.  
  197. /* Вызовем функцию несколько раз */
  198. $amount1 = mt_rand(1,99999999);
  199. $text1 = numberToText($amount1);
  200. echo "На вашем счету {$text1} \n";
  201.  
  202. $amount2 = mt_rand(1,99999999);
  203. $text2 = numberToText($amount2);
  204. echo "На вашем счету {$text2} \n";
  205.  
  206. $amount3 = mt_rand(1,99999999);
  207. $text3 = numberToText($amount3);
  208. echo "На вашем счету {$text3} \n";
  209.  
  210. $amount4 = mt_rand(1,99999999);
  211. $text4 = numberToText($amount4);
  212. echo "На вашем счету {$text4} \n";
  213.  
  214. $amount5 = mt_rand(1,9);
  215. $text5 = numberToText($amount5);
  216. echo "На вашем счету {$text5} \n";
  217.  
  218. $amount6 = 9009;
  219. $text6 = numberToText($amount6);
  220. echo "На вашем счету {$text6} \n";
  221.  
  222. $amount7 = 999999999;
  223. $text7 = numberToText($amount7);
  224. echo "На вашем счету {$text7} \n";
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
Исходное число 93566960
На вашем счету  девять квадриллионов три триллиона пять миллиардов шесть миллионов шесть тысячь девятьсот шестьдесят рублей  
Исходное число 49424029
На вашем счету  четыре квадриллиона девять триллионов четыре миллиарда два миллиона четыре тысячи ноль два рублей  
Исходное число 39917850
На вашем счету  три квадриллиона девять триллионов девять миллиардов один миллион семь тысячь восемьсот пятьдесят рублей  
Исходное число 30549047
На вашем счету  три квадриллиона ноль триллион пять миллиардов четыре миллиона девять тысячь ноль четыре рублей  
Исходное число 5
На вашем счету  пять рублей  
Исходное число 9009
На вашем счету  девять тысячь  рублей  
Исходное число 999999999
На вашем счету  девять квинтиллионов девять квадриллионов девять триллионов девять миллиардов девять миллионов девять тысячь девятьсот девяносто девять рублей