3v4l.org

run code in 300+ PHP versions simultaneously
<?php $document = "<html> <a href='do not keep'><div>DO NOT KEEP1</div></a> <a href='do keep' data-link='keepLink'>Keep Link</a> <a href='do not keep'>DO NOT KEEP2</a> </html>"; $dom = new DOMDocument; $dom->loadXml($document); // load the file $xpath = new DOMXPath($dom); $nodes = $xpath->query('//a[not(@data-link=\'keepLink\')]'); // search for links that do not have the 'data-link' attribute set to 'keepLink' foreach($nodes as $element){ $textInside = $element->nodeValue; // get the text inside the link $parentNode = $element->parentNode; // save parent node $parentNode->replaceChild(new DOMText($textInside), $element); // remove the element } $myNewHTML = $dom->saveHTML(); // see http://php.net/manual/ro/domdocument.savehtml.php for limitations such as auto-adding of doc-type echo $myNewHTML;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 29
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 29
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/ejatQ
function name:  (null)
number of ops:  35
compiled vars:  !0 = $document, !1 = $dom, !2 = $xpath, !3 = $nodes, !4 = $element, !5 = $textInside, !6 = $parentNode, !7 = $myNewHTML
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Chtml%3E%0A%3Ca+href%3D%27do+not+keep%27%3E%3Cdiv%3EDO+NOT+KEEP1%3C%2Fdiv%3E%3C%2Fa%3E%0A%0A%3Ca+href%3D%27do+keep%27+data-link%3D%27keepLink%27%3EKeep+Link%3C%2Fa%3E%0A%0A%3Ca+href%3D%27do+not+keep%27%3EDO+NOT+KEEP2%3C%2Fa%3E%0A%3C%2Fhtml%3E'
   11     1        NEW                                              $9      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $9
   12     4        INIT_METHOD_CALL                                         !1, 'loadXml'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   14     7        NEW                                              $13     'DOMXPath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $13
   15    11        INIT_METHOD_CALL                                         !2, 'query'
         12        SEND_VAL_EX                                              '%2F%2Fa%5Bnot%28%40data-link%3D%27keepLink%27%29%5D'
         13        DO_FCALL                                      0  $16     
         14        ASSIGN                                                   !3, $16
   17    15      > FE_RESET_R                                       $18     !3, ->29
         16    > > FE_FETCH_R                                               $18, !4, ->29
   18    17    >   FETCH_OBJ_R                                      ~19     !4, 'nodeValue'
         18        ASSIGN                                                   !5, ~19
   19    19        FETCH_OBJ_R                                      ~21     !4, 'parentNode'
         20        ASSIGN                                                   !6, ~21
   20    21        INIT_METHOD_CALL                                         !6, 'replaceChild'
         22        NEW                                              $23     'DOMText'
         23        SEND_VAR_EX                                              !5
         24        DO_FCALL                                      0          
         25        SEND_VAR_NO_REF_EX                                       $23
         26        SEND_VAR_EX                                              !4
         27        DO_FCALL                                      0          
   17    28      > JMP                                                      ->16
         29    >   FE_FREE                                                  $18
   23    30        INIT_METHOD_CALL                                         !1, 'saveHTML'
         31        DO_FCALL                                      0  $26     
         32        ASSIGN                                                   !7, $26
   25    33        ECHO                                                     !7
         34      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.69 ms | 1000 KiB | 13 Q