3v4l.org

run code in 300+ PHP versions simultaneously
<?php $content = <<< 'HTML' This is an outside Example <p href="https://example.com"> This is a para Example</p> test <markup class="m"> this is a markup example</markup> another example <p>example</p> HTML; // Initialize a DOM object $dom = new DOMDocument(); @$dom->loadHTML("<div>$content</div>"); // Use an HTML element tag as our HTML container // @hakre [https://stackoverflow.com/a/29499718/1020526] $wrapper = $dom->getElementsByTagName('div')->item(0); // Remove wrapper $wrapper = $wrapper->parentNode->removeChild($wrapper); // Remove all nodes of $dom object while ($dom->firstChild) { $dom->removeChild($dom->firstChild); } // Append all $wrapper object nodes to $dom while ($wrapper->firstChild) { $dom->appendChild($wrapper->firstChild); } $dox = new DOMXPath($dom); // Query all elements in addition to text nodes $query = $dox->query('/* | /text()'); // Iterate through all nodes foreach ($query as $node) { // If it's not an HTML element if ($node->nodeType != XML_ELEMENT_NODE) { // Replace desired word / content $newContent = preg_replace('~(example)~i', '<markup>$1</markup>', $node->wholeText); // We can't insert HTML directly into our node // so we need to create a document fragment $newNode = $dom->createDocumentFragment(); $newNode->appendXML($newContent); // Replace new content with old one $node->parentNode->replaceChild($newNode, $node); } } // Save modifications echo $dom->saveHTML($dom);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 25
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 33
Branch analysis from position: 40
2 jumps found. (Code = 77) Position 1 = 49, Position 2 = 72
Branch analysis from position: 49
2 jumps found. (Code = 78) Position 1 = 50, Position 2 = 72
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 71
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
Branch analysis from position: 71
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 72
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 33
Branch analysis from position: 40
Branch analysis from position: 33
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 25
Branch analysis from position: 32
Branch analysis from position: 25
filename:       /in/2CBun
function name:  (null)
number of ops:  78
compiled vars:  !0 = $content, !1 = $dom, !2 = $wrapper, !3 = $dox, !4 = $query, !5 = $node, !6 = $newContent, !7 = $newNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'This+is+an+outside+Example+%3Cp+href%3D%22https%3A%2F%2Fexample.com%22%3E+This+is+a+para+Example%3C%2Fp%3E+test+%3Cmarkup+class%3D%22m%22%3E+this+is+a+markup+example%3C%2Fmarkup%3E+another+example+%3Cp%3Eexample%3C%2Fp%3E'
    8     1        NEW                                              $9      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $9
    9     4        BEGIN_SILENCE                                    ~12     
          5        INIT_METHOD_CALL                                         !1, 'loadHTML'
          6        ROPE_INIT                                     3  ~14     '%3Cdiv%3E'
          7        ROPE_ADD                                      1  ~14     ~14, !0
          8        ROPE_END                                      2  ~13     ~14, '%3C%2Fdiv%3E'
          9        SEND_VAL_EX                                              ~13
         10        DO_FCALL                                      0          
         11        END_SILENCE                                              ~12
   13    12        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
         13        SEND_VAL_EX                                              'div'
         14        DO_FCALL                                      0  $17     
         15        INIT_METHOD_CALL                                         $17, 'item'
         16        SEND_VAL_EX                                              0
         17        DO_FCALL                                      0  $18     
         18        ASSIGN                                                   !2, $18
   15    19        FETCH_OBJ_R                                      ~20     !2, 'parentNode'
         20        INIT_METHOD_CALL                                         ~20, 'removeChild'
         21        SEND_VAR_EX                                              !2
         22        DO_FCALL                                      0  $21     
         23        ASSIGN                                                   !2, $21
   17    24      > JMP                                                      ->30
   18    25    >   INIT_METHOD_CALL                                         !1, 'removeChild'
         26        CHECK_FUNC_ARG                                           
         27        FETCH_OBJ_FUNC_ARG                               $23     !1, 'firstChild'
         28        SEND_FUNC_ARG                                            $23
         29        DO_FCALL                                      0          
   17    30    >   FETCH_OBJ_R                                      ~25     !1, 'firstChild'
         31      > JMPNZ                                                    ~25, ->25
   21    32    > > JMP                                                      ->38
   22    33    >   INIT_METHOD_CALL                                         !1, 'appendChild'
         34        CHECK_FUNC_ARG                                           
         35        FETCH_OBJ_FUNC_ARG                               $26     !2, 'firstChild'
         36        SEND_FUNC_ARG                                            $26
         37        DO_FCALL                                      0          
   21    38    >   FETCH_OBJ_R                                      ~28     !2, 'firstChild'
         39      > JMPNZ                                                    ~28, ->33
   25    40    >   NEW                                              $29     'DOMXPath'
         41        SEND_VAR_EX                                              !1
         42        DO_FCALL                                      0          
         43        ASSIGN                                                   !3, $29
   27    44        INIT_METHOD_CALL                                         !3, 'query'
         45        SEND_VAL_EX                                              '%2F%2A+%7C+%2Ftext%28%29'
         46        DO_FCALL                                      0  $32     
         47        ASSIGN                                                   !4, $32
   30    48      > FE_RESET_R                                       $34     !4, ->72
         49    > > FE_FETCH_R                                               $34, !5, ->72
   32    50    >   FETCH_OBJ_R                                      ~35     !5, 'nodeType'
         51        IS_NOT_EQUAL                                             ~35, 1
         52      > JMPZ                                                     ~36, ->71
   34    53    >   INIT_FCALL                                               'preg_replace'
         54        SEND_VAL                                                 '%7E%28example%29%7Ei'
         55        SEND_VAL                                                 '%3Cmarkup%3E%241%3C%2Fmarkup%3E'
         56        FETCH_OBJ_R                                      ~37     !5, 'wholeText'
         57        SEND_VAL                                                 ~37
         58        DO_ICALL                                         $38     
         59        ASSIGN                                                   !6, $38
   37    60        INIT_METHOD_CALL                                         !1, 'createDocumentFragment'
         61        DO_FCALL                                      0  $40     
         62        ASSIGN                                                   !7, $40
   38    63        INIT_METHOD_CALL                                         !7, 'appendXML'
         64        SEND_VAR_EX                                              !6
         65        DO_FCALL                                      0          
   40    66        FETCH_OBJ_R                                      ~43     !5, 'parentNode'
         67        INIT_METHOD_CALL                                         ~43, 'replaceChild'
         68        SEND_VAR_EX                                              !7
         69        SEND_VAR_EX                                              !5
         70        DO_FCALL                                      0          
   30    71    > > JMP                                                      ->49
         72    >   FE_FREE                                                  $34
   45    73        INIT_METHOD_CALL                                         !1, 'saveHTML'
         74        SEND_VAR_EX                                              !1
         75        DO_FCALL                                      0  $45     
         76        ECHO                                                     $45
         77      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.96 ms | 1019 KiB | 14 Q