3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Ingreso $html = ' <div class="try">foo</div> <div>foo</div> foo <span>foo</span>'; echo "TEXTO ORIGINAL:" . $html; //Generar el DOM $doc = new DOMDocument; $doc->loadHTML($html, LIBXML_COMPACT | LIBXML_NONET); $body = $doc->getElementsByTagName('body')->item(0); //Reemplazar recursivamente $html = buscarNodoDOM($body, "foo", "bar", "try"); function buscarNodoDOM(DOMNode $nodoDOM, $buscar, $reemplazo, $claseProhibida, $recursivo = false) { $resultado = ""; //recorrer todos los nodos foreach ($nodoDOM->childNodes as $nodo) { if ($nodo->nodeType == XML_TEXT_NODE) { //Si es un nodo de texto, reemplazar $nodo->nodeValue = str_ireplace( $buscar, $reemplazo, $nodo->nodeValue); } elseif(( !$nodo->hasAttributes() || !in_array($claseProhibida, explode(" ", $nodo->getAttribute("class"))) ) && $nodo->hasChildNodes()) { //si no es de la clase prohibida, y tiene hijos, recorrer todos los hijos buscarNodoDOM($nodo, $buscar, $reemplazo, $claseProhibida, true); } if (!$recursivo) { //si estamos en el body, agregar todo el HTML del nodo al resultado $resultado .= $nodoDOM->ownerDocument->saveHTML($nodo); } } //devolver el HTML como texto return $resultado; } //Mostrar resultado echo "\n\nTEXTO MODIFICADO:\n" . $html;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JSeV5
function name:  (null)
number of ops:  27
compiled vars:  !0 = $html, !1 = $doc, !2 = $body
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, '%0A%3Cdiv+class%3D%22try%22%3Efoo%3C%2Fdiv%3E%0A%3Cdiv%3Efoo%3C%2Fdiv%3E%0Afoo%0A%3Cspan%3Efoo%3C%2Fspan%3E'
   10     1        CONCAT                                           ~4      'TEXTO+ORIGINAL%3A', !0
          2        ECHO                                                     ~4
   14     3        NEW                                              $5      'DOMDocument'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $5
   15     6        INIT_METHOD_CALL                                         !1, 'loadHTML'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAL_EX                                              67584
          9        DO_FCALL                                      0          
   16    10        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
         11        SEND_VAL_EX                                              'body'
         12        DO_FCALL                                      0  $9      
         13        INIT_METHOD_CALL                                         $9, 'item'
         14        SEND_VAL_EX                                              0
         15        DO_FCALL                                      0  $10     
         16        ASSIGN                                                   !2, $10
   20    17        INIT_FCALL_BY_NAME                                       'buscarNodoDOM'
         18        SEND_VAR_EX                                              !2
         19        SEND_VAL_EX                                              'foo'
         20        SEND_VAL_EX                                              'bar'
         21        SEND_VAL_EX                                              'try'
         22        DO_FCALL                                      0  $12     
         23        ASSIGN                                                   !0, $12
   49    24        CONCAT                                           ~14     '%0A%0ATEXTO+MODIFICADO%3A%0A', !0
         25        ECHO                                                     ~14
         26      > RETURN                                                   1

Function buscarnododom:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 58
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 58
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 21
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 57
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 57
Branch analysis from position: 21
2 jumps found. (Code = 47) Position 1 = 25, Position 2 = 38
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 39, Position 2 = 42
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 50
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 57
Branch analysis from position: 52
Branch analysis from position: 57
Branch analysis from position: 50
Branch analysis from position: 42
Branch analysis from position: 38
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 58
filename:       /in/JSeV5
function name:  buscarNodoDOM
number of ops:  61
compiled vars:  !0 = $nodoDOM, !1 = $buscar, !2 = $reemplazo, !3 = $claseProhibida, !4 = $recursivo, !5 = $resultado, !6 = $nodo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV_INIT                                        !4      <false>
   23     5        ASSIGN                                                   !5, ''
   26     6        FETCH_OBJ_R                                      ~8      !0, 'childNodes'
          7      > FE_RESET_R                                       $9      ~8, ->58
          8    > > FE_FETCH_R                                               $9, !6, ->58
   28     9    >   FETCH_OBJ_R                                      ~10     !6, 'nodeType'
         10        IS_EQUAL                                                 ~10, 3
         11      > JMPZ                                                     ~11, ->21
   30    12    >   INIT_FCALL                                               'str_ireplace'
         13        SEND_VAR                                                 !1
         14        SEND_VAR                                                 !2
         15        FETCH_OBJ_R                                      ~13     !6, 'nodeValue'
         16        SEND_VAL                                                 ~13
         17        DO_ICALL                                         $14     
         18        ASSIGN_OBJ                                               !6, 'nodeValue'
         19        OP_DATA                                                  $14
   28    20      > JMP                                                      ->50
   31    21    >   INIT_METHOD_CALL                                         !6, 'hasAttributes'
         22        DO_FCALL                                      0  $15     
         23        BOOL_NOT                                         ~16     $15
         24      > JMPNZ_EX                                         ~16     ~16, ->38
         25    >   INIT_FCALL                                               'in_array'
         26        SEND_VAR                                                 !3
         27        INIT_FCALL                                               'explode'
         28        SEND_VAL                                                 '+'
         29        INIT_METHOD_CALL                                         !6, 'getAttribute'
         30        SEND_VAL_EX                                              'class'
         31        DO_FCALL                                      0  $17     
         32        SEND_VAR                                                 $17
         33        DO_ICALL                                         $18     
         34        SEND_VAR                                                 $18
         35        DO_ICALL                                         $19     
         36        BOOL_NOT                                         ~20     $19
         37        BOOL                                             ~16     ~20
         38    > > JMPZ_EX                                          ~16     ~16, ->42
   32    39    >   INIT_METHOD_CALL                                         !6, 'hasChildNodes'
         40        DO_FCALL                                      0  $21     
         41        BOOL                                             ~16     $21
         42    > > JMPZ                                                     ~16, ->50
   34    43    >   INIT_FCALL_BY_NAME                                       'buscarNodoDOM'
         44        SEND_VAR_EX                                              !6
         45        SEND_VAR_EX                                              !1
         46        SEND_VAR_EX                                              !2
         47        SEND_VAR_EX                                              !3
         48        SEND_VAL_EX                                              <true>
         49        DO_FCALL                                      0          
   37    50    >   BOOL_NOT                                         ~23     !4
         51      > JMPZ                                                     ~23, ->57
   39    52    >   FETCH_OBJ_R                                      ~24     !0, 'ownerDocument'
         53        INIT_METHOD_CALL                                         ~24, 'saveHTML'
         54        SEND_VAR_EX                                              !6
         55        DO_FCALL                                      0  $25     
         56        ASSIGN_OP                                     8          !5, $25
   26    57    > > JMP                                                      ->8
         58    >   FE_FREE                                                  $9
   43    59      > RETURN                                                   !5
   44    60*     > RETURN                                                   null

End of function buscarnododom

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.98 ms | 1016 KiB | 16 Q