3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string_old = "This is the second div that we are using to check if the original data can be seen & Yes It is Working"; $string_new = "This is the first div that we are using to check if the changed data can be seen & Yes It is Working"; function getDiffed($string_old, $string_new){ $A = explode(" ", $string_old); $B = explode(" ", $string_new); $LCS = []; for($i = 0; $i < count($A); ++$i){ for($j = 0; $j < count($B); ++$j){ if($A[ $i ] == $B[ $j ]){ $LCS[ $i ][ $j ] = max($LCS[ $i ][ $j - 1 ] ?? 0, ($LCS[ $i - 1 ][ $j - 1 ] ?? 0) + 1); }else{ $LCS[ $i ][ $j ] = max($LCS[ $i - 1 ][ $j ] ?? 0, $LCS[ $i ][ $j - 1 ] ?? 0); } } } // get the LCS string space separated. $i = count($A) - 1; $j = count($B) - 1; $commonWords = []; while($i >= 0 && $j >= 0){ if($A[ $i ] == $B[ $j ]){ $commonWords[] = $A[ $i ]; $i--; $j--; }else if(($LCS[ $i ][$j - 1] ?? 0) > ($LCS[ $i - 1 ][ $j ] ?? 0)){ $j--; }else{ $i--; } } $commonWords = array_reverse($commonWords); // add red marks if any for the old string for($i = 0, $ptr = 0; $i < count($A); ++$i){ if($ptr == count($commonWords) || $A[ $i ] !== $commonWords[ $ptr ]){ $A[ $i ] = "<del style='background-color:#ffcccc'> " .$A[ $i ] . "</del>"; }else{ $ptr++; } } // add green marks if any for the new string for($i = 0, $ptr = 0; $i < count($B); ++$i){ if($ptr == count($commonWords) || $B[ $i ] !== $commonWords[ $ptr ]){ $B[ $i ] = "<ins style='background-color:#ccffcc'> " .$B[ $i ] . "</ins>"; }else{ $ptr++; } } return ['old' => implode(' ', $A), 'new' => implode(' ', $B)]; } $diff = getDiffed($string_old, $string_new); $doc = <<<DOC <center> <h2>Old String VS New String</h2> <table border=1 cellpadding=15> <tr align=center> <td>Old</td> <td>New</td> </tr> <tr> <td>$diff[old]</td> <td>$diff[new]</td> </tr> </table> </center> DOC; echo $doc;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LVN7S
function name:  (null)
number of ops:  17
compiled vars:  !0 = $string_old, !1 = $string_new, !2 = $diff, !3 = $doc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'This+is+the+second+div+that+we+are+using+to+check+if+the+original+data+can+be+seen+%26+Yes+It+is+Working'
    4     1        ASSIGN                                                   !1, 'This+is+the+first+div+that+we+are+using+to+check+if+the+changed+data+can+be+seen+%26+Yes+It+is+Working'
   64     2        INIT_FCALL                                               'getdiffed'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !2, $6
   67     7        ROPE_INIT                                     5  ~11     '%3Ccenter%3E%0A%3Ch2%3EOld+String+VS+New+String%3C%2Fh2%3E%0A%3Ctable+border%3D1+cellpadding%3D15%3E%0A++++%3Ctr+align%3Dcenter%3E%0A++++++++%3Ctd%3EOld%3C%2Ftd%3E%0A++++++++%3Ctd%3ENew%3C%2Ftd%3E%0A++++%3C%2Ftr%3E%0A++++%3Ctr%3E%0A++++++++%3Ctd%3E'
   75     8        FETCH_DIM_R                                      ~8      !2, 'old'
          9        ROPE_ADD                                      1  ~11     ~11, ~8
         10        ROPE_ADD                                      2  ~11     ~11, '%3C%2Ftd%3E%0A++++++++%3Ctd%3E'
   76    11        FETCH_DIM_R                                      ~9      !2, 'new'
         12        ROPE_ADD                                      3  ~11     ~11, ~9
         13        ROPE_END                                      4  ~10     ~11, '%3C%2Ftd%3E%0A++++%3C%2Ftr%3E%0A%3C%2Ftable%3E%0A%3C%2Fcenter%3E'
   66    14        ASSIGN                                                   !3, ~10
   82    15        ECHO                                                     !3
   83    16      > RETURN                                                   1

Function getdiffed:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
2 jumps found. (Code = 44) Position 1 = 66, Position 2 = 15
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
2 jumps found. (Code = 46) Position 1 = 101, Position 2 = 103
Branch analysis from position: 101
2 jumps found. (Code = 44) Position 1 = 104, Position 2 = 74
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 127
Branch analysis from position: 127
2 jumps found. (Code = 44) Position 1 = 130, Position 2 = 111
Branch analysis from position: 130
1 jumps found. (Code = 42) Position 1 = 149
Branch analysis from position: 149
2 jumps found. (Code = 44) Position 1 = 152, Position 2 = 133
Branch analysis from position: 152
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 133
2 jumps found. (Code = 47) Position 1 = 136, Position 2 = 140
Branch analysis from position: 136
2 jumps found. (Code = 43) Position 1 = 141, Position 2 = 147
Branch analysis from position: 141
1 jumps found. (Code = 42) Position 1 = 148
Branch analysis from position: 148
2 jumps found. (Code = 44) Position 1 = 152, Position 2 = 133
Branch analysis from position: 152
Branch analysis from position: 133
Branch analysis from position: 147
2 jumps found. (Code = 44) Position 1 = 152, Position 2 = 133
Branch analysis from position: 152
Branch analysis from position: 133
Branch analysis from position: 140
Branch analysis from position: 111
2 jumps found. (Code = 47) Position 1 = 114, Position 2 = 118
Branch analysis from position: 114
2 jumps found. (Code = 43) Position 1 = 119, Position 2 = 125
Branch analysis from position: 119
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
2 jumps found. (Code = 44) Position 1 = 130, Position 2 = 111
Branch analysis from position: 130
Branch analysis from position: 111
Branch analysis from position: 125
2 jumps found. (Code = 44) Position 1 = 130, Position 2 = 111
Branch analysis from position: 130
Branch analysis from position: 111
Branch analysis from position: 118
Branch analysis from position: 74
2 jumps found. (Code = 43) Position 1 = 78, Position 2 = 84
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
Branch analysis from position: 84
2 jumps found. (Code = 43) Position 1 = 96, Position 2 = 98
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
Branch analysis from position: 98
2 jumps found. (Code = 46) Position 1 = 101, Position 2 = 103
Branch analysis from position: 101
Branch analysis from position: 103
Branch analysis from position: 103
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 17
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 66, Position 2 = 15
Branch analysis from position: 66
Branch analysis from position: 15
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 41
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 17
Branch analysis from position: 62
Branch analysis from position: 17
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 17
Branch analysis from position: 62
Branch analysis from position: 17
filename:       /in/LVN7S
function name:  getDiffed
number of ops:  164
compiled vars:  !0 = $string_old, !1 = $string_new, !2 = $A, !3 = $B, !4 = $LCS, !5 = $i, !6 = $j, !7 = $commonWords, !8 = $ptr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !2, $9
    8     7        INIT_FCALL                                               'explode'
          8        SEND_VAL                                                 '+'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $11     
         11        ASSIGN                                                   !3, $11
   10    12        ASSIGN                                                   !4, <array>
   12    13        ASSIGN                                                   !5, 0
         14      > JMP                                                      ->63
   13    15    >   ASSIGN                                                   !6, 0
         16      > JMP                                                      ->59
   14    17    >   FETCH_DIM_R                                      ~16     !2, !5
         18        FETCH_DIM_R                                      ~17     !3, !6
         19        IS_EQUAL                                                 ~16, ~17
         20      > JMPZ                                                     ~18, ->41
   15    21    >   INIT_FCALL                                               'max'
         22        SUB                                              ~22     !6, 1
         23        FETCH_DIM_IS                                     ~21     !4, !5
         24        FETCH_DIM_IS                                     ~23     ~21, ~22
         25        COALESCE                                         ~24     ~23
         26        QM_ASSIGN                                        ~24     0
         27        SEND_VAL                                                 ~24
         28        SUB                                              ~25     !5, 1
         29        SUB                                              ~27     !6, 1
         30        FETCH_DIM_IS                                     ~26     !4, ~25
         31        FETCH_DIM_IS                                     ~28     ~26, ~27
         32        COALESCE                                         ~29     ~28
         33        QM_ASSIGN                                        ~29     0
         34        ADD                                              ~30     ~29, 1
         35        SEND_VAL                                                 ~30
         36        DO_ICALL                                         $31     
         37        FETCH_DIM_W                                      $19     !4, !5
         38        ASSIGN_DIM                                               $19, !6
         39        OP_DATA                                                  $31
   14    40      > JMP                                                      ->58
   17    41    >   INIT_FCALL                                               'max'
         42        SUB                                              ~34     !5, 1
         43        FETCH_DIM_IS                                     ~35     !4, ~34
         44        FETCH_DIM_IS                                     ~36     ~35, !6
         45        COALESCE                                         ~37     ~36
         46        QM_ASSIGN                                        ~37     0
         47        SEND_VAL                                                 ~37
         48        SUB                                              ~39     !6, 1
         49        FETCH_DIM_IS                                     ~38     !4, !5
         50        FETCH_DIM_IS                                     ~40     ~38, ~39
         51        COALESCE                                         ~41     ~40
         52        QM_ASSIGN                                        ~41     0
         53        SEND_VAL                                                 ~41
         54        DO_ICALL                                         $42     
         55        FETCH_DIM_W                                      $32     !4, !5
         56        ASSIGN_DIM                                               $32, !6
         57        OP_DATA                                                  $42
   13    58    >   PRE_INC                                                  !6
         59    >   COUNT                                            ~44     !3
         60        IS_SMALLER                                               !6, ~44
         61      > JMPNZ                                                    ~45, ->17
   12    62    >   PRE_INC                                                  !5
         63    >   COUNT                                            ~47     !2
         64        IS_SMALLER                                               !5, ~47
         65      > JMPNZ                                                    ~48, ->15
   24    66    >   COUNT                                            ~49     !2
         67        SUB                                              ~50     ~49, 1
         68        ASSIGN                                                   !5, ~50
   25    69        COUNT                                            ~52     !3
         70        SUB                                              ~53     ~52, 1
         71        ASSIGN                                                   !6, ~53
   26    72        ASSIGN                                                   !7, <array>
   28    73      > JMP                                                      ->99
   29    74    >   FETCH_DIM_R                                      ~56     !2, !5
         75        FETCH_DIM_R                                      ~57     !3, !6
         76        IS_EQUAL                                                 ~56, ~57
         77      > JMPZ                                                     ~58, ->84
   30    78    >   FETCH_DIM_R                                      ~60     !2, !5
         79        ASSIGN_DIM                                               !7
         80        OP_DATA                                                  ~60
   31    81        PRE_DEC                                                  !5
   32    82        PRE_DEC                                                  !6
   29    83      > JMP                                                      ->99
   33    84    >   SUB                                              ~64     !6, 1
         85        FETCH_DIM_IS                                     ~63     !4, !5
         86        FETCH_DIM_IS                                     ~65     ~63, ~64
         87        COALESCE                                         ~66     ~65
         88        QM_ASSIGN                                        ~66     0
         89        SUB                                              ~67     !5, 1
         90        FETCH_DIM_IS                                     ~68     !4, ~67
         91        FETCH_DIM_IS                                     ~69     ~68, !6
         92        COALESCE                                         ~70     ~69
         93        QM_ASSIGN                                        ~70     0
         94        IS_SMALLER                                               ~70, ~66
         95      > JMPZ                                                     ~71, ->98
   34    96    >   PRE_DEC                                                  !6
   33    97      > JMP                                                      ->99
   36    98    >   PRE_DEC                                                  !5
   28    99    >   IS_SMALLER_OR_EQUAL                              ~74     0, !5
        100      > JMPZ_EX                                          ~74     ~74, ->103
        101    >   IS_SMALLER_OR_EQUAL                              ~75     0, !6
        102        BOOL                                             ~74     ~75
        103    > > JMPNZ                                                    ~74, ->74
   40   104    >   INIT_FCALL                                               'array_reverse'
        105        SEND_VAR                                                 !7
        106        DO_ICALL                                         $76     
        107        ASSIGN                                                   !7, $76
   43   108        ASSIGN                                                   !5, 0
        109        ASSIGN                                                   !8, 0
        110      > JMP                                                      ->127
   44   111    >   COUNT                                            ~80     !7
        112        IS_EQUAL                                         ~81     !8, ~80
        113      > JMPNZ_EX                                         ~81     ~81, ->118
        114    >   FETCH_DIM_R                                      ~82     !2, !5
        115        FETCH_DIM_R                                      ~83     !7, !8
        116        IS_NOT_IDENTICAL                                 ~84     ~82, ~83
        117        BOOL                                             ~81     ~84
        118    > > JMPZ                                                     ~81, ->125
   45   119    >   FETCH_DIM_R                                      ~86     !2, !5
        120        CONCAT                                           ~87     '%3Cdel+style%3D%27background-color%3A%23ffcccc%27%3E+', ~86
        121        CONCAT                                           ~88     ~87, '%3C%2Fdel%3E'
        122        ASSIGN_DIM                                               !2, !5
        123        OP_DATA                                                  ~88
   44   124      > JMP                                                      ->126
   47   125    >   PRE_INC                                                  !8
   43   126    >   PRE_INC                                                  !5
        127    >   COUNT                                            ~91     !2
        128        IS_SMALLER                                               !5, ~91
        129      > JMPNZ                                                    ~92, ->111
   52   130    >   ASSIGN                                                   !5, 0
        131        ASSIGN                                                   !8, 0
        132      > JMP                                                      ->149
   53   133    >   COUNT                                            ~95     !7
        134        IS_EQUAL                                         ~96     !8, ~95
        135      > JMPNZ_EX                                         ~96     ~96, ->140
        136    >   FETCH_DIM_R                                      ~97     !3, !5
        137        FETCH_DIM_R                                      ~98     !7, !8
        138        IS_NOT_IDENTICAL                                 ~99     ~97, ~98
        139        BOOL                                             ~96     ~99
        140    > > JMPZ                                                     ~96, ->147
   54   141    >   FETCH_DIM_R                                      ~101    !3, !5
        142        CONCAT                                           ~102    '%3Cins+style%3D%27background-color%3A%23ccffcc%27%3E+', ~101
        143        CONCAT                                           ~103    ~102, '%3C%2Fins%3E'
        144        ASSIGN_DIM                                               !3, !5
        145        OP_DATA                                                  ~103
   53   146      > JMP                                                      ->148
   56   147    >   PRE_INC                                                  !8
   52   148    >   PRE_INC                                                  !5
        149    >   COUNT                                            ~106    !3
        150        IS_SMALLER                                               !5, ~106
        151      > JMPNZ                                                    ~107, ->133
   61   152    >   INIT_FCALL                                               'implode'
        153        SEND_VAL                                                 '+'
        154        SEND_VAR                                                 !2
        155        DO_ICALL                                         $108    
        156        INIT_ARRAY                                       ~109    $108, 'old'
        157        INIT_FCALL                                               'implode'
        158        SEND_VAL                                                 '+'
        159        SEND_VAR                                                 !3
        160        DO_ICALL                                         $110    
        161        ADD_ARRAY_ELEMENT                                ~109    $110, 'new'
        162      > RETURN                                                   ~109
   62   163*     > RETURN                                                   null

End of function getdiffed

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.14 ms | 1033 KiB | 18 Q