3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <div class="somethingelse">content</div> <div class="product-fields"> <div class="product-field product-field-type-R"> <span class="product-fields-title-wrapper"><span class="product-fields-title"><strong>Related Products</strong></span> <span title="" class="hasTooltip" data-original-title="&lt;strong&gt;Related Products&lt;/strong&gt;&lt;br /&gt;COM_VIRTUEMART_RELATED_PRODUCTS_TIP"><img alt="Tooltip" src="/j34/media/system/images/tooltip.png"></span></span> <span class="product-field-display"><a target="blank" title="Test Product 1" href="/j34/index.php/shops/c1/t1-detail">Test Product 1</a></span><span class="product-field-desc">Custom prototype for related products</span> </div> <div class="mickmackusa">Not the drone you're looking for</div> <div class="product-field matrix-agent-smith product-field-type-R"> <span>Find them and destroy them</span> </div> </div> <div class="someothercontainer">Text</div> HTML; $getRelatedProduct = "Flashy, shiny, new"; libxml_use_internal_errors(true); $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $element = $dom->createElement('div', $getRelatedProduct); $xpath = new DOMXPath($dom); $nodes = $xpath->query("//div[contains(@class, 'product-fields')]/div[contains(@class, 'product-field') and contains(@class, 'product-field-type-R')]"); for ($i = $nodes->length - 1; $i >= 0; --$i) { // when removing nodes from DOM, work in reverse order for stability $node = $nodes->item($i); if ($i) { //$trailing_whitespace = $node->nextSibling; $node->parentNode->removeChild($node); // remove any qualifying element that IS NOT the first element //$trailing_whitespace->parentNode->removeChild($trailing_whitespace); // optionally remove line returns after removed tags } else { $node->parentNode->replaceChild($element, $node); // replace the first element with the generated element } } echo $dom->saveHTML();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 29
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 39
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 29
Branch analysis from position: 47
Branch analysis from position: 29
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 29
Branch analysis from position: 47
Branch analysis from position: 29
filename:       /in/guEQL
function name:  (null)
number of ops:  51
compiled vars:  !0 = $html, !1 = $getRelatedProduct, !2 = $dom, !3 = $element, !4 = $xpath, !5 = $nodes, !6 = $i, !7 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, '%3Cdiv+class%3D%22somethingelse%22%3Econtent%3C%2Fdiv%3E%0A%3Cdiv+class%3D%22product-fields%22%3E%0A++++%3Cdiv+class%3D%22product-field+product-field-type-R%22%3E%0A++++++++%3Cspan+class%3D%22product-fields-title-wrapper%22%3E%3Cspan+class%3D%22product-fields-title%22%3E%3Cstrong%3ERelated+Products%3C%2Fstrong%3E%3C%2Fspan%3E%0A++++++++%3Cspan+title%3D%22%22+class%3D%22hasTooltip%22+data-original-title%3D%22%26lt%3Bstrong%26gt%3BRelated+Products%26lt%3B%2Fstrong%26gt%3B%26lt%3Bbr+%2F%26gt%3BCOM_VIRTUEMART_RELATED_PRODUCTS_TIP%22%3E%3Cimg+alt%3D%22Tooltip%22+src%3D%22%2Fj34%2Fmedia%2Fsystem%2Fimages%2Ftooltip.png%22%3E%3C%2Fspan%3E%3C%2Fspan%3E%0A++++++++%3Cspan+class%3D%22product-field-display%22%3E%3Ca+target%3D%22blank%22+title%3D%22Test+Product+1%22+href%3D%22%2Fj34%2Findex.php%2Fshops%2Fc1%2Ft1-detail%22%3ETest+Product+1%3C%2Fa%3E%3C%2Fspan%3E%3Cspan+class%3D%22product-field-desc%22%3ECustom+prototype+for+related+products%3C%2Fspan%3E++++%0A++++%3C%2Fdiv%3E%0A++++%3Cdiv+class%3D%22mickmackusa%22%3ENot+the+drone+you%27re+looking+for%3C%2Fdiv%3E%0A++++%3Cdiv+class%3D%22product-field+matrix-agent-smith+product-field-type-R%22%3E%0A++++++++%3Cspan%3EFind+them+and+destroy+them%3C%2Fspan%3E%0A++++%3C%2Fdiv%3E%0A%3C%2Fdiv%3E%0A%3Cdiv+class%3D%22someothercontainer%22%3EText%3C%2Fdiv%3E'
   18     1        ASSIGN                                                   !1, 'Flashy%2C+shiny%2C+new'
   20     2        INIT_FCALL                                               'libxml_use_internal_errors'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                                 
   21     5        NEW                                              $11     'DOMDocument'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !2, $11
   22     8        INIT_METHOD_CALL                                         !2, 'loadHTML'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAL_EX                                              8196
         11        DO_FCALL                                      0          
   23    12        INIT_METHOD_CALL                                         !2, 'createElement'
         13        SEND_VAL_EX                                              'div'
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0  $15     
         16        ASSIGN                                                   !3, $15
   25    17        NEW                                              $17     'DOMXPath'
         18        SEND_VAR_EX                                              !2
         19        DO_FCALL                                      0          
         20        ASSIGN                                                   !4, $17
   26    21        INIT_METHOD_CALL                                         !4, 'query'
         22        SEND_VAL_EX                                              '%2F%2Fdiv%5Bcontains%28%40class%2C+%27product-fields%27%29%5D%2Fdiv%5Bcontains%28%40class%2C+%27product-field%27%29+and+contains%28%40class%2C+%27product-field-type-R%27%29%5D'
         23        DO_FCALL                                      0  $20     
         24        ASSIGN                                                   !5, $20
   27    25        FETCH_OBJ_R                                      ~22     !5, 'length'
         26        SUB                                              ~23     ~22, 1
         27        ASSIGN                                                   !6, ~23
         28      > JMP                                                      ->45
   28    29    >   INIT_METHOD_CALL                                         !5, 'item'
         30        SEND_VAR_EX                                              !6
         31        DO_FCALL                                      0  $25     
         32        ASSIGN                                                   !7, $25
   29    33      > JMPZ                                                     !6, ->39
   31    34    >   FETCH_OBJ_R                                      ~27     !7, 'parentNode'
         35        INIT_METHOD_CALL                                         ~27, 'removeChild'
         36        SEND_VAR_EX                                              !7
         37        DO_FCALL                                      0          
         38      > JMP                                                      ->44
   34    39    >   FETCH_OBJ_R                                      ~29     !7, 'parentNode'
         40        INIT_METHOD_CALL                                         ~29, 'replaceChild'
         41        SEND_VAR_EX                                              !3
         42        SEND_VAR_EX                                              !7
         43        DO_FCALL                                      0          
   27    44    >   PRE_DEC                                                  !6
         45    >   IS_SMALLER_OR_EQUAL                                      0, !6
         46      > JMPNZ                                                    ~32, ->29
   38    47    >   INIT_METHOD_CALL                                         !2, 'saveHTML'
         48        DO_FCALL                                      0  $33     
         49        ECHO                                                     $33
         50      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.54 ms | 1405 KiB | 15 Q