Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.55 KB | None | 0 0
  1. <?php
  2. /*
  3.  *
  4.  */
  5.  
  6. // Staring straight up into the sky ... oh my my
  7. error_reporting(-1);
  8. mb_internal_encoding('utf-8');
  9.  
  10.  
  11. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  12. function inclineWord($number, $word1, $word2, $word5) {
  13.     /* DIY */
  14. }
  15. //функция выбора формы слова
  16. function getWordForm($number, $order){
  17.     $formsRub = array("рубль","рубля","рублей");
  18.     $formsThousand = array("тысяча","тысячи","тысяч");
  19.     $formsMill = array("миллион","миллиона","миллионов");
  20.     $forms = array($formsRub, $formsThousand, $formsMill);
  21.     if($number > 99){
  22.         $number-=floor($number/100)*100;        
  23.     }
  24.         if (($number > 4) and ($number < 21)) {
  25.         return $forms[$order][2];        
  26.     }
  27.     $num = floor($number%10);
  28.     if($num == 1){
  29.         return $forms[$order][0];
  30.     }
  31.     if((($num > 1 )and($num < 5))){
  32.         return $forms[$order][1];
  33.     }
  34.     if(($num > 4 )and($num < 11)or($num == 0)){
  35.         return $forms[$order][2];
  36.     }
  37. }
  38.  
  39.  
  40. //принимает на вход число от 0 до 999 и возвращает его текстовую форму
  41. function spellSmallNumber($number, $isFemale){
  42.     $num = $number;
  43.     $words = array();
  44.     if($num==0){
  45.         array_push($words, smallNumberToText($num, $isFemale));
  46.         return implode($words," ");        
  47.     }
  48.     if ($num > 99){
  49.             $order = floor($num/100)*100;
  50.             array_push($words, smallNumberToText($order, $isFemale));
  51.             $num -= $order;            
  52.         }
  53.     if (($num > 0)and($num < 11)){
  54.         array_push($words, smallNumberToText($num, $isFemale));
  55.         return implode($words," ");
  56.     }
  57.     if (($num > 10)and($num < 20)){
  58.         array_push($words, smallNumberToText($num, $isFemale));
  59.         return implode($words," ");
  60.     }
  61.     if ($num > 19){
  62.             $order = floor($num/10)*10;
  63.             array_push($words, smallNumberToText($order, $isFemale));
  64.             $num -= $order;
  65.             if($num==0){return implode($words," ");}
  66.             array_push($words, smallNumberToText($num, $isFemale));
  67.             return implode($words," ");
  68.         }    
  69. }
  70. /*
  71.     Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  72.     если мы считаем число для мужского рода (один рубль),
  73.     и 1 — для женского (одна тысяча)
  74. */
  75. function smallNumberToText($number, $isFemale) {
  76.  
  77.     $spelling = array(
  78.         0   =>  'ноль',                                     10  =>  'десять',       100 =>  'сто',
  79.         1   =>  'один',         11  =>  'одиннадцать',      20  =>  'двадцать',     200 =>  'двести',
  80.         2   =>  'два',          12  =>  'двенадцать',       30  =>  'тридцать',     300 =>  'триста',
  81.         3   =>  'три',          13  =>  'тринадцать',       40  =>  'сорок',        400 =>  'четыреста',
  82.         4   =>  'четыре',       14  =>  'четырнадцать',     50  =>  'пятьдесят',    500 =>  'пятьсот',
  83.         5   =>  'пять',         15  =>  'пятнадцать',       60  =>  'шестьдесят',   600 =>  'шестьсот',
  84.         6   =>  'шесть',        16  =>  'шестнадцать',      70  =>  'семьдесят',    700 =>  'семьсот',    
  85.         7   =>  'семь',         17  =>  'семнадцать',       80  =>  'восемьдесят',   800 =>  'восемьсот',
  86.         8   =>  'восемь',       18  =>  'восемнадцать',     90  =>  'девяносто',     900 =>  'девятьсот',
  87.         9   =>  'девять',       19  =>  'девятнадцать'    
  88.     );
  89.  
  90.     $femaleSpelling = array(
  91.         1   =>  'одна',        2   =>  'две'
  92.     );
  93.  
  94.     /* DIY */
  95.     if ($isFemale == 1){
  96.         $spelling = array_replace($spelling, $femaleSpelling);
  97.        
  98.     }
  99.    
  100.     $text = $spelling[$number];  
  101.    
  102.     return $text;
  103. }
  104.  
  105. function numberToText($number) {
  106.     $result = array();
  107.     $numbers = array_reverse(explode(" ", number_format($number, 0, ',', ' ')));    
  108.     foreach ($numbers as $key => $value) {
  109.         if (($key == 1) and ((int)$value == 0)){
  110.             continue;
  111.         }
  112.         if ((count($numbers)>1)and($value==0)){  
  113.             $wordForm = getWordForm((int)$value, $key);
  114.             array_push($result, $wordForm);
  115.             continue;
  116.         }
  117.         $smallNum = spellSmallNumber((int)$value, $key);
  118.         $wordForm = getWordForm((int)$value, $key);
  119.         $array = array($smallNum, $wordForm);
  120.        
  121.         array_push($result, implode(" ", $array));
  122.        
  123.     }
  124.     $result = array_reverse($result);
  125.     return implode(" ", $result);
  126.      
  127. }
  128.  
  129. // Вызовем функцию несколько раз
  130.  
  131. $amount1 = mt_rand(1,99999999);
  132. $text1 = numberToText($amount1);
  133.  
  134. echo "На вашем счету {$text1}<br>";
  135.  
  136. $amount2 = mt_rand(1,99999999);
  137. $text2 = numberToText($amount2);
  138.  
  139. echo "На вашем счету {$text2}<br>";
  140.  
  141. $amount3 = mt_rand(1,99999999);
  142. $text3 = numberToText($amount3);
  143.  
  144. echo "На вашем счету {$text3}<br>";
  145.  
  146. $amount4 = mt_rand(1,99999999);
  147. $text4 = numberToText($amount4);
  148.  
  149. echo "На вашем счету {$text4}<br>";
  150.  
  151.  
  152. /*
  153. $number = 7000000;
  154. echo numberToText($number); */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement