3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); mb_internal_encoding('utf-8'); /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */ //создадим массивы в массиве, чтобы можно было искать по типу $массив[k][i] где //k это тип слова (зависит от обрабатываемой тройки цифр), а i рассчитывается по алгоритму{ function inclineWord($number, $type) { $type1 = array( 0 => 'миллион', 1 => 'миллиона', 2 => 'миллионов' ); $type2 = array( 0 => 'тысяча', 1 => 'тысячи', 2 => 'тысяч' ); $type3 = array( 0 => 'рубль.', 1 => 'рубля.', 2 => 'рублей.' ); $union = array( 0 => $type1, 1 => $type2, 2 => $type3 ); $i = getFinalWord($number); return ($union[$type][$i]); } //} function smallNumberToText($number, $isFemale) { $spelling = array( 0 => '', 10 => 'десять', 100 => 'сто', 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести', 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста', 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста', 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот', 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот', 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот', 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот', 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот', 9 => 'девять', 19 => 'девятнадцать' ); $femaleSpelling = array( 1 => 'одна', 2 => 'две' ); $words = array(); //сотни{ if ($number > 99){ $words[0] = $spelling[(floor($number/100))*100]; } //} //две последних цифры, у которых может быть своё общее название{ $check = $number % 100; if ($check >= 10 && $check <= 19){ $words[1] = $spelling[$check]; //если общего названия нет, то работаем с каждой цифрой по отдельности } else { //десятки if ((floor($check/10))*10 <> 0){ $words[1] = $spelling[(floor($check/10))*10]; } //единицы if ($check % 10 <> 0){ if($isFemale == true && ($check % 10 <= 2)){ $words[2] = $femaleSpelling[$check % 10]; }else{ $words[2] = $spelling[$check % 10]; } } } //} $text = implode(" ", $words); return $text; } //разбиваем исходное число на тройки и помещаем в массив{ function spellSmallNumber($number){ $numbers = array(); $numbers[0] = floor($number / 1000000); $numbers[1] = floor(($number % 1000000)/1000); $numbers[2] = $number % 1000; return ($numbers); } //} function numberToText($number) { $text = ""; //если цифра задана 0, но и считать её не нужно, пишем "ноль"{ if ($number <> 0){ $numbers = spellSmallNumber($number); //$count будет использоваться в массиве $union для поиска словоформ, при проходе каждой тройки увеличивается //0 - миллионы, 1 - тысячи, 2 - сотни{ $count = 0; //} //будем писать все слова, составляющие цифру, в один финальный массив //$i будет служить для счетчика ключа{ $i = 0; $finalArray = array(); //} foreach ($numbers as $oneNumber) { //женский род только у тысячи, т.е. у типа 1{ if ($count == 1){ $isFemale = true; }else{ $isFemale = false; } //} //если группа из 3х цифр равна 0, то нет смысла искать ей слово разрядности и вообще превращать в слово if ($oneNumber <> 0){ $finalArray[$i] = smallNumberToText($oneNumber, $isFemale); $i++; //если это последняя группа (сотни), то слова разрядности искать не нужно if ($count <> 2){ $finalArray[$i] = inclineWord($oneNumber, $count); $i++; } //} } //} $count++; } $text = implode(" ", $finalArray); } else { $text = "ноль"; } //} return $text; } //работа с поиском нужной словоформы. для этого определим 2 последних числа //и если они не в промежутке от 5 до 20, то работаем с последней цифрой{ function getFinalWord($number){ if ($number >= 5 && $number <= 20){ return(2); } elseif($number >= 21){ $number = $number%10; return(checking($number)); } else{ return(checking($number)); } } //} function checking($number){ if ($number == 1){ return(0); }elseif($number >= 2 && $number <= 4){ return(1); }else{ return(2); } } //$amount1 = mt_rand(1,99999999); $amount1 = 7000000; echo "{$amount1}\n"; $text1 = numberToText($amount1); //ищем, какую форму надо поставить у слова "рубль"{ $finalWord = inclineWord($amount1 % 100, 2); //} echo "На вашем счету: {$text1} {$finalWord}\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VG05G
function name:  (null)
number of ops:  27
compiled vars:  !0 = $amount1, !1 = $text1, !2 = $finalWord
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 -1
          2        DO_ICALL                                                 
    4     3        INIT_FCALL                                               'mb_internal_encoding'
          4        SEND_VAL                                                 'utf-8'
          5        DO_ICALL                                                 
  159     6        ASSIGN                                                   !0, 7000000
  160     7        NOP                                                      
          8        FAST_CONCAT                                      ~6      !0, '%0A'
          9        ECHO                                                     ~6
  161    10        INIT_FCALL                                               'numbertotext'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $7      
         13        ASSIGN                                                   !1, $7
  163    14        INIT_FCALL                                               'inclineword'
         15        MOD                                              ~9      !0, 100
         16        SEND_VAL                                                 ~9
         17        SEND_VAL                                                 2
         18        DO_FCALL                                      0  $10     
         19        ASSIGN                                                   !2, $10
  166    20        ROPE_INIT                                     5  ~13     '%D0%9D%D0%B0+%D0%B2%D0%B0%D1%88%D0%B5%D0%BC+%D1%81%D1%87%D0%B5%D1%82%D1%83%3A+'
         21        ROPE_ADD                                      1  ~13     ~13, !1
         22        ROPE_ADD                                      2  ~13     ~13, '+'
         23        ROPE_ADD                                      3  ~13     ~13, !2
         24        ROPE_END                                      4  ~12     ~13, '%0A'
         25        ECHO                                                     ~12
         26      > RETURN                                                   1

Function inclineword:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VG05G
function name:  inclineWord
number of ops:  17
compiled vars:  !0 = $number, !1 = $type, !2 = $type1, !3 = $type2, !4 = $type3, !5 = $union, !6 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        ASSIGN                                                   !2, <array>
   14     3        ASSIGN                                                   !3, <array>
   17     4        ASSIGN                                                   !4, <array>
   22     5        INIT_ARRAY                                       ~10     !2, 0
          6        ADD_ARRAY_ELEMENT                                ~10     !3, 1
          7        ADD_ARRAY_ELEMENT                                ~10     !4, 2
   21     8        ASSIGN                                                   !5, ~10
   25     9        INIT_FCALL_BY_NAME                                       'getFinalWord'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $12     
         12        ASSIGN                                                   !6, $12
   26    13        FETCH_DIM_R                                      ~14     !5, !1
         14        FETCH_DIM_R                                      ~15     ~14, !6
         15      > RETURN                                                   ~15
   27    16*     > RETURN                                                   null

End of function inclineword

Function smallnumbertotext:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 26
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 41
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 59
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 49
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 55
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
Branch analysis from position: 59
Branch analysis from position: 41
Branch analysis from position: 21
Branch analysis from position: 15
filename:       /in/VG05G
function name:  smallNumberToText
number of ops:  66
compiled vars:  !0 = $number, !1 = $isFemale, !2 = $spelling, !3 = $femaleSpelling, !4 = $words, !5 = $check, !6 = $text
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        ASSIGN                                                   !2, <array>
   45     3        ASSIGN                                                   !3, <array>
   49     4        ASSIGN                                                   !4, <array>
   51     5        IS_SMALLER                                               99, !0
          6      > JMPZ                                                     ~10, ->15
   52     7    >   INIT_FCALL                                               'floor'
          8        DIV                                              ~12     !0, 100
          9        SEND_VAL                                                 ~12
         10        DO_ICALL                                         $13     
         11        MUL                                              ~14     $13, 100
         12        FETCH_DIM_R                                      ~15     !2, ~14
         13        ASSIGN_DIM                                               !4, 0
         14        OP_DATA                                                  ~15
   56    15    >   MOD                                              ~16     !0, 100
         16        ASSIGN                                                   !5, ~16
   57    17        IS_SMALLER_OR_EQUAL                              ~18     10, !5
         18      > JMPZ_EX                                          ~18     ~18, ->21
         19    >   IS_SMALLER_OR_EQUAL                              ~19     !5, 19
         20        BOOL                                             ~18     ~19
         21    > > JMPZ                                                     ~18, ->26
   58    22    >   FETCH_DIM_R                                      ~21     !2, !5
         23        ASSIGN_DIM                                               !4, 1
         24        OP_DATA                                                  ~21
         25      > JMP                                                      ->59
   62    26    >   INIT_FCALL                                               'floor'
         27        DIV                                              ~22     !5, 10
         28        SEND_VAL                                                 ~22
         29        DO_ICALL                                         $23     
         30        MUL                                              ~24     $23, 10
         31        IS_NOT_EQUAL                                             ~24, 0
         32      > JMPZ                                                     ~25, ->41
   63    33    >   INIT_FCALL                                               'floor'
         34        DIV                                              ~27     !5, 10
         35        SEND_VAL                                                 ~27
         36        DO_ICALL                                         $28     
         37        MUL                                              ~29     $28, 10
         38        FETCH_DIM_R                                      ~30     !2, ~29
         39        ASSIGN_DIM                                               !4, 1
         40        OP_DATA                                                  ~30
   66    41    >   MOD                                              ~31     !5, 10
         42        IS_NOT_EQUAL                                             ~31, 0
         43      > JMPZ                                                     ~32, ->59
   67    44    >   BOOL                                             ~33     !1
         45      > JMPZ_EX                                          ~33     ~33, ->49
         46    >   MOD                                              ~34     !5, 10
         47        IS_SMALLER_OR_EQUAL                              ~35     ~34, 2
         48        BOOL                                             ~33     ~35
         49    > > JMPZ                                                     ~33, ->55
   68    50    >   MOD                                              ~37     !5, 10
         51        FETCH_DIM_R                                      ~38     !3, ~37
         52        ASSIGN_DIM                                               !4, 2
         53        OP_DATA                                                  ~38
         54      > JMP                                                      ->59
   70    55    >   MOD                                              ~40     !5, 10
         56        FETCH_DIM_R                                      ~41     !2, ~40
         57        ASSIGN_DIM                                               !4, 2
         58        OP_DATA                                                  ~41
   75    59    >   INIT_FCALL                                               'implode'
         60        SEND_VAL                                                 '+'
         61        SEND_VAR                                                 !4
         62        DO_ICALL                                         $42     
         63        ASSIGN                                                   !6, $42
   76    64      > RETURN                                                   !6
   77    65*     > RETURN                                                   null

End of function smallnumbertotext

Function spellsmallnumber:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VG05G
function name:  spellSmallNumber
number of ops:  20
compiled vars:  !0 = $number, !1 = $numbers
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   81     1        ASSIGN                                                   !1, <array>
   82     2        INIT_FCALL                                               'floor'
          3        DIV                                              ~4      !0, 1000000
          4        SEND_VAL                                                 ~4
          5        DO_ICALL                                         $5      
          6        ASSIGN_DIM                                               !1, 0
          7        OP_DATA                                                  $5
   83     8        INIT_FCALL                                               'floor'
          9        MOD                                              ~7      !0, 1000000
         10        DIV                                              ~8      ~7, 1000
         11        SEND_VAL                                                 ~8
         12        DO_ICALL                                         $9      
         13        ASSIGN_DIM                                               !1, 1
         14        OP_DATA                                                  $9
   84    15        MOD                                              ~11     !0, 1000
         16        ASSIGN_DIM                                               !1, 2
         17        OP_DATA                                                  ~11
   85    18      > RETURN                                                   !1
   86    19*     > RETURN                                                   null

End of function spellsmallnumber

Function numbertotext:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 45
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 38
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 38
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 36
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 36
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 36
Branch analysis from position: 36
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 36
Branch analysis from position: 20
Branch analysis from position: 36
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VG05G
function name:  numberToText
number of ops:  48
compiled vars:  !0 = $number, !1 = $text, !2 = $numbers, !3 = $count, !4 = $i, !5 = $finalArray, !6 = $oneNumber, !7 = $isFemale
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
   90     1        ASSIGN                                                   !1, ''
   92     2        IS_NOT_EQUAL                                             !0, 0
          3      > JMPZ                                                     ~9, ->45
   93     4    >   INIT_FCALL                                               'spellsmallnumber'
          5        SEND_VAR                                                 !0
          6        DO_FCALL                                      0  $10     
          7        ASSIGN                                                   !2, $10
   96     8        ASSIGN                                                   !3, 0
  100     9        ASSIGN                                                   !4, 0
  101    10        ASSIGN                                                   !5, <array>
  103    11      > FE_RESET_R                                       $15     !2, ->38
         12    > > FE_FETCH_R                                               $15, !6, ->38
  105    13    >   IS_EQUAL                                                 !3, 1
         14      > JMPZ                                                     ~16, ->17
  106    15    >   ASSIGN                                                   !7, <true>
         16      > JMP                                                      ->18
  108    17    >   ASSIGN                                                   !7, <false>
  112    18    >   IS_NOT_EQUAL                                             !6, 0
         19      > JMPZ                                                     ~19, ->36
  113    20    >   INIT_FCALL                                               'smallnumbertotext'
         21        SEND_VAR                                                 !6
         22        SEND_VAR                                                 !7
         23        DO_FCALL                                      0  $21     
         24        ASSIGN_DIM                                               !5, !4
         25        OP_DATA                                                  $21
  114    26        PRE_INC                                                  !4
  116    27        IS_NOT_EQUAL                                             !3, 2
         28      > JMPZ                                                     ~23, ->36
  117    29    >   INIT_FCALL                                               'inclineword'
         30        SEND_VAR                                                 !6
         31        SEND_VAR                                                 !3
         32        DO_FCALL                                      0  $25     
         33        ASSIGN_DIM                                               !5, !4
         34        OP_DATA                                                  $25
  118    35        PRE_INC                                                  !4
  123    36    >   PRE_INC                                                  !3
  103    37      > JMP                                                      ->12
         38    >   FE_FREE                                                  $15
  125    39        INIT_FCALL                                               'implode'
         40        SEND_VAL                                                 '+'
         41        SEND_VAR                                                 !5
         42        DO_ICALL                                         $28     
         43        ASSIGN                                                   !1, $28
         44      > JMP                                                      ->46
  127    45    >   ASSIGN                                                   !1, '%D0%BD%D0%BE%D0%BB%D1%8C'
  131    46    > > RETURN                                                   !1
  132    47*     > RETURN                                                   null

End of function numbertotext

Function getfinalword:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 17
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/VG05G
function name:  getFinalWord
number of ops:  22
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   RECV                                             !0      
  137     1        IS_SMALLER_OR_EQUAL                              ~1      5, !0
          2      > JMPZ_EX                                          ~1      ~1, ->5
          3    >   IS_SMALLER_OR_EQUAL                              ~2      !0, 20
          4        BOOL                                             ~1      ~2
          5    > > JMPZ                                                     ~1, ->8
  138     6    > > RETURN                                                   2
          7*       JMP                                                      ->21
  139     8    >   IS_SMALLER_OR_EQUAL                                      21, !0
          9      > JMPZ                                                     ~3, ->17
  140    10    >   MOD                                              ~4      !0, 10
         11        ASSIGN                                                   !0, ~4
  141    12        INIT_FCALL_BY_NAME                                       'checking'
         13        SEND_VAR_EX                                              !0
         14        DO_FCALL                                      0  $6      
         15      > RETURN                                                   $6
         16*       JMP                                                      ->21
  143    17    >   INIT_FCALL_BY_NAME                                       'checking'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0  $7      
         20      > RETURN                                                   $7
  145    21*     > RETURN                                                   null

End of function getfinalword

Function checking:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/VG05G
function name:  checking
number of ops:  14
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  148     0  E >   RECV                                             !0      
  149     1        IS_EQUAL                                                 !0, 1
          2      > JMPZ                                                     ~1, ->5
  150     3    > > RETURN                                                   0
          4*       JMP                                                      ->13
  151     5    >   IS_SMALLER_OR_EQUAL                              ~2      2, !0
          6      > JMPZ_EX                                          ~2      ~2, ->9
          7    >   IS_SMALLER_OR_EQUAL                              ~3      !0, 4
          8        BOOL                                             ~2      ~3
          9    > > JMPZ                                                     ~2, ->12
  152    10    > > RETURN                                                   1
         11*       JMP                                                      ->13
  154    12    > > RETURN                                                   2
  156    13*     > RETURN                                                   null

End of function checking

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.67 ms | 1419 KiB | 26 Q