3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<HTML <div class="productabcont"> <div class="protabtop"> <div class="protabtopright"> <p>Combed compact yarn</p> <!-- This i want to replace with <h4> tag. --> <p>Description line 1</p> <p>Description line 2</p> </div> </div> </div> HTML; $value = 'some headline'; $dom = new DOMDocument(); $dom->loadHTML($html); $q = new DOMXPath($dom); // find first p tag foreach ($q->query('//p[1]') as $p) { // replace it with newly created element $p->parentNode->replaceChild($dom->createElement('h4', $value), $p); } echo $dom->saveHTML();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 27
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 27
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/4Hj1e
function name:  (null)
number of ops:  32
compiled vars:  !0 = $html, !1 = $value, !2 = $dom, !3 = $q, !4 = $p
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, '%3Cdiv+class%3D%22productabcont%22%3E%0A++++%3Cdiv+class%3D%22protabtop%22%3E%0A++++++++%3Cdiv+class%3D%22protabtopright%22%3E%0A++++++++++++%3Cp%3ECombed+compact+yarn%3C%2Fp%3E+%3C%21--+This+i+want+to+replace+with+%3Ch4%3E+tag.+--%3E%0A++++++++++++%3Cp%3EDescription+line+1%3C%2Fp%3E%0A++++++++++++%3Cp%3EDescription+line+2%3C%2Fp%3E%0A++++++++%3C%2Fdiv%3E%0A++++%3C%2Fdiv%3E++++++++++%0A%3C%2Fdiv%3E'
   15     1        ASSIGN                                                       !1, 'some+headline'
   17     2        NEW                                                  $7      'DOMDocument'
          3        DO_FCALL                                          0          
          4        ASSIGN                                                       !2, $7
   18     5        INIT_METHOD_CALL                                             !2, 'loadHTML'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
   20     8        NEW                                                  $11     'DOMXPath'
          9        SEND_VAR_EX                                                  !2
         10        DO_FCALL                                          0          
         11        ASSIGN                                                       !3, $11
   23    12        INIT_METHOD_CALL                                             !3, 'query'
         13        SEND_VAL_EX                                                  '%2F%2Fp%5B1%5D'
         14        DO_FCALL                                          0  $14     
         15      > FE_RESET_R                                           $15     $14, ->27
         16    > > FE_FETCH_R                                                   $15, !4, ->27
   25    17    >   FETCH_OBJ_R                                          ~16     !4, 'parentNode'
         18        INIT_METHOD_CALL                                             ~16, 'replaceChild'
         19        INIT_METHOD_CALL                                             !2, 'createElement'
         20        SEND_VAL_EX                                                  'h4'
         21        SEND_VAR_EX                                                  !1
         22        DO_FCALL                                          0  $17     
         23        SEND_VAR_NO_REF_EX                                           $17
         24        SEND_VAR_EX                                                  !4
         25        DO_FCALL                                          0          
   23    26      > JMP                                                          ->16
         27    >   FE_FREE                                                      $15
   28    28        INIT_METHOD_CALL                                             !2, 'saveHTML'
         29        DO_FCALL                                          0  $19     
         30        ECHO                                                         $19
         31      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170.45 ms | 2741 KiB | 13 Q