3v4l.org

run code in 300+ PHP versions simultaneously
<?php function roman2number($roman){ $conv = array( array("letter" => 'I', "number" => 1), array("letter" => 'V', "number" => 5), array("letter" => 'X', "number" => 10), array("letter" => 'L', "number" => 50), array("letter" => 'C', "number" => 100), array("letter" => 'D', "number" => 500), array("letter" => 'M', "number" => 1000), array("letter" => 0, "number" => 0) ); $arabic = 0; $state = 0; $sidx = 0; $len = strlen($roman); while ($len >= 0) { $i = 0; $sidx = $len; while ($conv[$i]['number'] > 0) { if (strtoupper(@$roman[$sidx]) == $conv[$i]['letter']) { if ($state > $conv[$i]['number']) { $arabic -= $conv[$i]['number']; } else { $arabic += $conv[$i]['number']; $state = $conv[$i]['number']; } } $i++; } $len--; } return($arabic); } $str = "This is IV chapter of book Hero and a preview of page MLCX"; echo preg_replace_callback('~\b[IVXLCDM0]+\b~', function($m) { return roman2number($m[0]); }, $str);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q1IHC
function name:  (null)
number of ops:  9
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   ASSIGN                                                   !0, 'This+is+IV+chapter+of+book+Hero+and+a+preview+of+page+MLCX'
   40     1        INIT_FCALL                                               'preg_replace_callback'
          2        SEND_VAL                                                 '%7E%5Cb%5BIVXLCDM0%5D%2B%5Cb%7E'
          3        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
   42     4        SEND_VAL                                                 ~2
          5        SEND_VAR                                                 !0
   40     6        DO_ICALL                                         $3      
   42     7        ECHO                                                     $3
          8      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q1IHC
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        INIT_FCALL                                               'roman2number'
          2        FETCH_DIM_R                                      ~1      !0, 0
          3        SEND_VAL                                                 ~1
          4        DO_FCALL                                      0  $2      
          5      > RETURN                                                   $2
   42     6*     > RETURN                                                   null

End of Dynamic Function 0

Function roman2number:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 8
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 11
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 8
Branch analysis from position: 43
Branch analysis from position: 8
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 35
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 11
Branch analysis from position: 40
Branch analysis from position: 11
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 11
Branch analysis from position: 40
Branch analysis from position: 11
Branch analysis from position: 35
filename:       /in/Q1IHC
function name:  roman2number
number of ops:  45
compiled vars:  !0 = $roman, !1 = $conv, !2 = $arabic, !3 = $state, !4 = $sidx, !5 = $len, !6 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, <array>
   14     2        ASSIGN                                                   !2, 0
   15     3        ASSIGN                                                   !3, 0
   16     4        ASSIGN                                                   !4, 0
   17     5        STRLEN                                           ~11     !0
          6        ASSIGN                                                   !5, ~11
   19     7      > JMP                                                      ->41
   20     8    >   ASSIGN                                                   !6, 0
   21     9        ASSIGN                                                   !4, !5
   22    10      > JMP                                                      ->36
   23    11    >   INIT_FCALL                                               'strtoupper'
         12        BEGIN_SILENCE                                    ~15     
         13        FETCH_DIM_R                                      ~16     !0, !4
         14        END_SILENCE                                              ~15
         15        SEND_VAL                                                 ~16
         16        DO_ICALL                                         $17     
         17        FETCH_DIM_R                                      ~18     !1, !6
         18        FETCH_DIM_R                                      ~19     ~18, 'letter'
         19        IS_EQUAL                                                 $17, ~19
         20      > JMPZ                                                     ~20, ->35
   24    21    >   FETCH_DIM_R                                      ~21     !1, !6
         22        FETCH_DIM_R                                      ~22     ~21, 'number'
         23        IS_SMALLER                                               ~22, !3
         24      > JMPZ                                                     ~23, ->29
   25    25    >   FETCH_DIM_R                                      ~24     !1, !6
         26        FETCH_DIM_R                                      ~25     ~24, 'number'
         27        ASSIGN_OP                                     2          !2, ~25
   24    28      > JMP                                                      ->35
   27    29    >   FETCH_DIM_R                                      ~27     !1, !6
         30        FETCH_DIM_R                                      ~28     ~27, 'number'
         31        ASSIGN_OP                                     1          !2, ~28
   28    32        FETCH_DIM_R                                      ~30     !1, !6
         33        FETCH_DIM_R                                      ~31     ~30, 'number'
         34        ASSIGN                                                   !3, ~31
   31    35    >   PRE_INC                                                  !6
   22    36    >   FETCH_DIM_R                                      ~34     !1, !6
         37        FETCH_DIM_R                                      ~35     ~34, 'number'
         38        IS_SMALLER                                               0, ~35
         39      > JMPNZ                                                    ~36, ->11
   33    40    >   PRE_DEC                                                  !5
   19    41    >   IS_SMALLER_OR_EQUAL                                      0, !5
         42      > JMPNZ                                                    ~38, ->8
   35    43    > > RETURN                                                   !2
   36    44*     > RETURN                                                   null

End of function roman2number

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.59 ms | 1019 KiB | 16 Q