3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<EOD <li id="" class="menu-item-has-children"> <a href="https://example.com">Parent Menu Item</a> <!-- Add button HTML element here --> <ul class="sub-menu"> <!-- Child menu items --> </ul> </li> EOD; $doc = new DOMDocument(); $doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($doc); foreach ($xpath->query('//li[@class="menu-item-has-children"]/a') as $a) { $button = $doc->createElement('button', 'Your button text'); // modify button as required // ... // add to document $a->parentNode->insertBefore($button, $a->nextSibling); } echo $doc->saveHTML();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 30
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 30
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/IDDus
function name:  (null)
number of ops:  35
compiled vars:  !0 = $html, !1 = $doc, !2 = $xpath, !3 = $a, !4 = $button
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cli+id%3D%22%22+class%3D%22menu-item-has-children%22%3E%0A++%3Ca+href%3D%22https%3A%2F%2Fexample.com%22%3EParent+Menu+Item%3C%2Fa%3E%0A++%3C%21--+Add+button+HTML+element+here+--%3E%0A++%3Cul+class%3D%22sub-menu%22%3E%0A++++%3C%21--+Child+menu+items+--%3E%0A++%3C%2Ful%3E%0A%3C%2Fli%3E'
   13     1        NEW                                              $6      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $6
   14     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAL_EX                                              8196
          7        DO_FCALL                                      0          
   15     8        NEW                                              $10     'DOMXPath'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !2, $10
   16    12        INIT_METHOD_CALL                                         !2, 'query'
         13        SEND_VAL_EX                                              '%2F%2Fli%5B%40class%3D%22menu-item-has-children%22%5D%2Fa'
         14        DO_FCALL                                      0  $13     
         15      > FE_RESET_R                                       $14     $13, ->30
         16    > > FE_FETCH_R                                               $14, !3, ->30
   17    17    >   INIT_METHOD_CALL                                         !1, 'createElement'
         18        SEND_VAL_EX                                              'button'
         19        SEND_VAL_EX                                              'Your+button+text'
         20        DO_FCALL                                      0  $15     
         21        ASSIGN                                                   !4, $15
   21    22        FETCH_OBJ_R                                      ~17     !3, 'parentNode'
         23        INIT_METHOD_CALL                                         ~17, 'insertBefore'
         24        SEND_VAR_EX                                              !4
         25        CHECK_FUNC_ARG                                           
         26        FETCH_OBJ_FUNC_ARG                               $18     !3, 'nextSibling'
         27        SEND_FUNC_ARG                                            $18
         28        DO_FCALL                                      0          
   16    29      > JMP                                                      ->16
         30    >   FE_FREE                                                  $14
   23    31        INIT_METHOD_CALL                                         !1, 'saveHTML'
         32        DO_FCALL                                      0  $20     
         33        ECHO                                                     $20
         34      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
193.05 ms | 998 KiB | 13 Q