3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = <<<END <table class="behaviourtable table"> <tr> <th>Existing column1</th> <th>Existing column2</th> <th>Existing column3</th> </tr><tr> <td>A</td> <td>B</td> <td>C</td> </tr><tr> <td>D</td> <td>E</td> <td>F</td> </tr><tr> <td class="text-info">G</td> </tr> </table> END; $dom = new DOMDocument();//Loads DOM document $dom->loadHTML($str);//Loads HTML from a previously set variable $xpath = new DOMXPath($dom); $tables = $xpath->query('//table[@class="behaviourtable table"]');//Get only table from HTML $commsTable = ''; foreach ($tables as $table) { $commsTable .= $dom->saveXML($table); } $commsHTML = new DOMDocument(); $commsHTML->loadHTML($commsTable); $tr = $commsHTML->getElementsByTagName('tr'); $th = $commsHTML->createElement('th', 'Comment'); $tr->item(0)->appendChild($th); $xpathcomms = new DOMXPath($commsHTML); $comments = $xpathcomms->query('//td[@class="text-info"]'); if($comments->length > 0){ foreach($comments as $comment){ $parent = $comment->parentNode; $parent->previousSibling->appendChild($comment); } } echo $commsHTML->saveHTML();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 23
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 66
Branch analysis from position: 56
2 jumps found. (Code = 77) Position 1 = 57, Position 2 = 65
Branch analysis from position: 57
2 jumps found. (Code = 78) Position 1 = 58, Position 2 = 65
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
Branch analysis from position: 66
Branch analysis from position: 23
filename:       /in/FZkNE
function name:  (null)
number of ops:  70
compiled vars:  !0 = $str, !1 = $dom, !2 = $xpath, !3 = $tables, !4 = $commsTable, !5 = $table, !6 = $commsHTML, !7 = $tr, !8 = $th, !9 = $xpathcomms, !10 = $comments, !11 = $comment, !12 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, '%3Ctable+class%3D%22behaviourtable+table%22%3E%0A++++%3Ctr%3E%0A++++++++%3Cth%3EExisting+column1%3C%2Fth%3E%0A++++++++%3Cth%3EExisting+column2%3C%2Fth%3E%0A++++++++%3Cth%3EExisting+column3%3C%2Fth%3E%0A++++%3C%2Ftr%3E%3Ctr%3E%0A++++++++%3Ctd%3EA%3C%2Ftd%3E%0A++++++++%3Ctd%3EB%3C%2Ftd%3E%0A++++++++%3Ctd%3EC%3C%2Ftd%3E%0A++++%3C%2Ftr%3E%3Ctr%3E%0A++++++++%3Ctd%3ED%3C%2Ftd%3E%0A++++++++%3Ctd%3EE%3C%2Ftd%3E%0A++++++++%3Ctd%3EF%3C%2Ftd%3E%0A++++%3C%2Ftr%3E%3Ctr%3E%0A++++++++%3Ctd+class%3D%22text-info%22%3EG%3C%2Ftd%3E%0A++++%3C%2Ftr%3E%0A%3C%2Ftable%3E'
   22     1        NEW                                              $14     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $14
   23     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   24     7        NEW                                              $18     'DOMXPath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $18
   25    11        INIT_METHOD_CALL                                         !2, 'query'
         12        SEND_VAL_EX                                              '%2F%2Ftable%5B%40class%3D%22behaviourtable+table%22%5D'
         13        DO_FCALL                                      0  $21     
         14        ASSIGN                                                   !3, $21
   26    15        ASSIGN                                                   !4, ''
   27    16      > FE_RESET_R                                       $24     !3, ->23
         17    > > FE_FETCH_R                                               $24, !5, ->23
   28    18    >   INIT_METHOD_CALL                                         !1, 'saveXML'
         19        SEND_VAR_EX                                              !5
         20        DO_FCALL                                      0  $25     
         21        ASSIGN_OP                                     8          !4, $25
   27    22      > JMP                                                      ->17
         23    >   FE_FREE                                                  $24
   31    24        NEW                                              $27     'DOMDocument'
         25        DO_FCALL                                      0          
         26        ASSIGN                                                   !6, $27
   32    27        INIT_METHOD_CALL                                         !6, 'loadHTML'
         28        SEND_VAR_EX                                              !4
         29        DO_FCALL                                      0          
   34    30        INIT_METHOD_CALL                                         !6, 'getElementsByTagName'
         31        SEND_VAL_EX                                              'tr'
         32        DO_FCALL                                      0  $31     
         33        ASSIGN                                                   !7, $31
   37    34        INIT_METHOD_CALL                                         !6, 'createElement'
         35        SEND_VAL_EX                                              'th'
         36        SEND_VAL_EX                                              'Comment'
         37        DO_FCALL                                      0  $33     
         38        ASSIGN                                                   !8, $33
   38    39        INIT_METHOD_CALL                                         !7, 'item'
         40        SEND_VAL_EX                                              0
         41        DO_FCALL                                      0  $35     
         42        INIT_METHOD_CALL                                         $35, 'appendChild'
         43        SEND_VAR_EX                                              !8
         44        DO_FCALL                                      0          
   40    45        NEW                                              $37     'DOMXPath'
         46        SEND_VAR_EX                                              !6
         47        DO_FCALL                                      0          
         48        ASSIGN                                                   !9, $37
   41    49        INIT_METHOD_CALL                                         !9, 'query'
         50        SEND_VAL_EX                                              '%2F%2Ftd%5B%40class%3D%22text-info%22%5D'
         51        DO_FCALL                                      0  $40     
         52        ASSIGN                                                   !10, $40
   42    53        FETCH_OBJ_R                                      ~42     !10, 'length'
         54        IS_SMALLER                                               0, ~42
         55      > JMPZ                                                     ~43, ->66
   43    56    > > FE_RESET_R                                       $44     !10, ->65
         57    > > FE_FETCH_R                                               $44, !11, ->65
   44    58    >   FETCH_OBJ_R                                      ~45     !11, 'parentNode'
         59        ASSIGN                                                   !12, ~45
   45    60        FETCH_OBJ_R                                      ~47     !12, 'previousSibling'
         61        INIT_METHOD_CALL                                         ~47, 'appendChild'
         62        SEND_VAR_EX                                              !11
         63        DO_FCALL                                      0          
   43    64      > JMP                                                      ->57
         65    >   FE_FREE                                                  $44
   49    66    >   INIT_METHOD_CALL                                         !6, 'saveHTML'
         67        DO_FCALL                                      0  $49     
         68        ECHO                                                     $49
         69      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.9 ms | 1399 KiB | 13 Q