3v4l.org

run code in 300+ PHP versions simultaneously
<?php // eliminar esta etiqueta usando expresiones regulares php // https://es.stackoverflow.com/q/131277/127 $html = ' <p><figure><img src="xxxx"></figure></p> <p> <figure> <img src="2222"> </figure> </p>'; //Generar el DOM $dom = new DOMDocument; $libxml_opciones = LIBXML_COMPACT | LIBXML_HTML_NODEFDTD | LIBXML_NONET | LIBXML_NOERROR; @$dom->loadHTML($html, $libxml_opciones); //Generar un XPath para búsquedas $xpath = new DOMXpath($dom); //Obtener todos los tags <p> $p_nodelist = $dom->getElementsByTagName('p'); //Bucle para cada <p> (en orden inverso para preservar la estructura) for ($i = $p_nodelist->length; --$i >= 0; ) { $p = $p_nodelist->item($i); $p_hijos = $xpath->query('./*',$p); //Si el <p> tiene un único hijo, y ese hijo es un <figure> if ($p_hijos->length == 1 && $p_hijos->item(0)->tagName == 'figure') { $figure = $p_hijos->item(0); //Si el <figure> tiene un único hijo, y ese hijo es una <img> $figure_hijos = $xpath->query('./*',$figure); if ($figure_hijos->length == 1 && $figure_hijos->item(0)->tagName == 'img') { //REEMPLAZAR todo el <p> por solamente el <figure> $p->parentNode->replaceChild($figure,$p); } } } //Guardar el html $resultado = ''; foreach ($dom->documentElement->lastChild->childNodes as $elem) { $resultado .= $dom->saveHTML($elem); } //Imprimir el resultado echo $resultado;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
2 jumps found. (Code = 44) Position 1 = 68, Position 2 = 22
Branch analysis from position: 68
2 jumps found. (Code = 77) Position 1 = 73, Position 2 = 79
Branch analysis from position: 73
2 jumps found. (Code = 78) Position 1 = 74, Position 2 = 79
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 79
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 79
Branch analysis from position: 22
2 jumps found. (Code = 46) Position 1 = 34, Position 2 = 40
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 65
Branch analysis from position: 41
2 jumps found. (Code = 46) Position 1 = 53, Position 2 = 59
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 65
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 68, Position 2 = 22
Branch analysis from position: 68
Branch analysis from position: 22
Branch analysis from position: 65
Branch analysis from position: 59
Branch analysis from position: 65
Branch analysis from position: 40
filename:       /in/Jcacn
function name:  (null)
number of ops:  82
compiled vars:  !0 = $html, !1 = $dom, !2 = $libxml_opciones, !3 = $xpath, !4 = $p_nodelist, !5 = $i, !6 = $p, !7 = $p_hijos, !8 = $figure, !9 = $figure_hijos, !10 = $resultado, !11 = $elem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, '%0A%3Cp%3E%3Cfigure%3E%3Cimg+src%3D%22xxxx%22%3E%3C%2Ffigure%3E%3C%2Fp%3E%0A%3Cp%3E%0A++%3Cfigure%3E%0A++++%3Cimg+src%3D%222222%22%3E%0A++%3C%2Ffigure%3E%0A%3C%2Fp%3E'
   14     1        NEW                                              $13     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $13
   15     4        ASSIGN                                                   !2, 67620
   16     5        BEGIN_SILENCE                                    ~17     
          6        INIT_METHOD_CALL                                         !1, 'loadHTML'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
         10        END_SILENCE                                              ~17
   18    11        NEW                                              $19     'DOMXpath'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !3, $19
   22    15        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
         16        SEND_VAL_EX                                              'p'
         17        DO_FCALL                                      0  $22     
         18        ASSIGN                                                   !4, $22
   25    19        FETCH_OBJ_R                                      ~24     !4, 'length'
         20        ASSIGN                                                   !5, ~24
         21      > JMP                                                      ->65
   26    22    >   INIT_METHOD_CALL                                         !4, 'item'
         23        SEND_VAR_EX                                              !5
         24        DO_FCALL                                      0  $26     
         25        ASSIGN                                                   !6, $26
   27    26        INIT_METHOD_CALL                                         !3, 'query'
         27        SEND_VAL_EX                                              '.%2F%2A'
         28        SEND_VAR_EX                                              !6
         29        DO_FCALL                                      0  $28     
         30        ASSIGN                                                   !7, $28
   30    31        FETCH_OBJ_R                                      ~30     !7, 'length'
         32        IS_EQUAL                                         ~31     ~30, 1
         33      > JMPZ_EX                                          ~31     ~31, ->40
         34    >   INIT_METHOD_CALL                                         !7, 'item'
         35        SEND_VAL_EX                                              0
         36        DO_FCALL                                      0  $32     
         37        FETCH_OBJ_R                                      ~33     $32, 'tagName'
         38        IS_EQUAL                                         ~34     ~33, 'figure'
         39        BOOL                                             ~31     ~34
         40    > > JMPZ                                                     ~31, ->65
   31    41    >   INIT_METHOD_CALL                                         !7, 'item'
         42        SEND_VAL_EX                                              0
         43        DO_FCALL                                      0  $35     
         44        ASSIGN                                                   !8, $35
   34    45        INIT_METHOD_CALL                                         !3, 'query'
         46        SEND_VAL_EX                                              '.%2F%2A'
         47        SEND_VAR_EX                                              !8
         48        DO_FCALL                                      0  $37     
         49        ASSIGN                                                   !9, $37
   35    50        FETCH_OBJ_R                                      ~39     !9, 'length'
         51        IS_EQUAL                                         ~40     ~39, 1
         52      > JMPZ_EX                                          ~40     ~40, ->59
         53    >   INIT_METHOD_CALL                                         !9, 'item'
         54        SEND_VAL_EX                                              0
         55        DO_FCALL                                      0  $41     
         56        FETCH_OBJ_R                                      ~42     $41, 'tagName'
         57        IS_EQUAL                                         ~43     ~42, 'img'
         58        BOOL                                             ~40     ~43
         59    > > JMPZ                                                     ~40, ->65
   38    60    >   FETCH_OBJ_R                                      ~44     !6, 'parentNode'
         61        INIT_METHOD_CALL                                         ~44, 'replaceChild'
         62        SEND_VAR_EX                                              !8
         63        SEND_VAR_EX                                              !6
         64        DO_FCALL                                      0          
   25    65    >   PRE_DEC                                          ~46     !5
         66        IS_SMALLER_OR_EQUAL                                      0, ~46
         67      > JMPNZ                                                    ~47, ->22
   45    68    >   ASSIGN                                                   !10, ''
   46    69        FETCH_OBJ_R                                      ~49     !1, 'documentElement'
         70        FETCH_OBJ_R                                      ~50     ~49, 'lastChild'
         71        FETCH_OBJ_R                                      ~51     ~50, 'childNodes'
         72      > FE_RESET_R                                       $52     ~51, ->79
         73    > > FE_FETCH_R                                               $52, !11, ->79
   47    74    >   INIT_METHOD_CALL                                         !1, 'saveHTML'
         75        SEND_VAR_EX                                              !11
         76        DO_FCALL                                      0  $53     
         77        ASSIGN_OP                                     8          !10, $53
   46    78      > JMP                                                      ->73
         79    >   FE_FREE                                                  $52
   51    80        ECHO                                                     !10
         81      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.21 ms | 1403 KiB | 13 Q