3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Modificar enlaces que contienen una imagen // https://es.stackoverflow.com/q/90027/127 //Un HTML de ejemplo $html = ' <a href="https://i.stack.imgur.com/mOJ0a.png"> <span>Enlace a la misma URL de la imagen</span> <img src="https://i.stack.imgur.com/mOJ0a.png" /> </a> <span>Imagen independiente precedida por un </span> <a href="https://i.stack.imgur.com/mOJ0a.png">enlace</a> <img src="https://i.stack.imgur.com/mOJ0a.png" /> <a href="./"> <span>Enlace a una URL diferente que la imagen</span> <img src="https://i.stack.imgur.com/mOJ0a.png" /> </a> '; //Englobamos en body $html = "<body>$html</body>"; //Generar el DOM $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_NONET | LIBXML_HTML_NODEFDTD); //Obtener todos los enlaces $a_nodelist = $dom->getElementsByTagName('a'); //Recorrer cada uno foreach ($a_nodelist as $enlace) { //Obtener la primera imagen dentro del enlace $img = $enlace->getElementsByTagName('img')->item(0); if ($img) { //si tiene imagen //Comparar el enlace con la imagen $urlEnlace = $enlace->getAttribute('href'); $urlImagen = $img->getAttribute('src'); if ($urlEnlace == $urlImagen) { //Si son el mismo, reemplazar $enlace->parentNode->replaceChild($img, $enlace); } } } //imprimir el resultado echo $dom->saveHTML();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 42
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 42
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 41
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 41
Branch analysis from position: 41
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/vOF7D
function name:  (null)
number of ops:  47
compiled vars:  !0 = $html, !1 = $dom, !2 = $a_nodelist, !3 = $enlace, !4 = $img, !5 = $urlEnlace, !6 = $urlImagen
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   ASSIGN                                                   !0, '%0A++++++++%3Ca+href%3D%22https%3A%2F%2Fi.stack.imgur.com%2FmOJ0a.png%22%3E%0A++++++++++++%3Cspan%3EEnlace+a+la+misma+URL+de+la+imagen%3C%2Fspan%3E%0A++++++++++++%3Cimg+src%3D%22https%3A%2F%2Fi.stack.imgur.com%2FmOJ0a.png%22+%2F%3E%0A++++++++%3C%2Fa%3E%0A%0A++++++++%3Cspan%3EImagen+independiente+precedida+por+un+%3C%2Fspan%3E%0A++++++++%3Ca+href%3D%22https%3A%2F%2Fi.stack.imgur.com%2FmOJ0a.png%22%3Eenlace%3C%2Fa%3E%0A++++++++%3Cimg+src%3D%22https%3A%2F%2Fi.stack.imgur.com%2FmOJ0a.png%22+%2F%3E%0A++++++++%0A++++++++%3Ca+href%3D%22.%2F%22%3E%0A++++++++++++%3Cspan%3EEnlace+a+una+URL+diferente+que+la+imagen%3C%2Fspan%3E%0A++++++++++++%3Cimg+src%3D%22https%3A%2F%2Fi.stack.imgur.com%2FmOJ0a.png%22+%2F%3E%0A++++++++%3C%2Fa%3E%0A'
   25     1        ROPE_INIT                                     3  ~9      '%3Cbody%3E'
          2        ROPE_ADD                                      1  ~9      ~9, !0
          3        ROPE_END                                      2  ~8      ~9, '%3C%2Fbody%3E'
          4        ASSIGN                                                   !0, ~8
   28     5        NEW                                              $12     'DOMDocument'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $12
   29     8        INIT_METHOD_CALL                                         !1, 'loadHTML'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAL_EX                                              75780
         11        DO_FCALL                                      0          
   33    12        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
         13        SEND_VAL_EX                                              'a'
         14        DO_FCALL                                      0  $16     
         15        ASSIGN                                                   !2, $16
   36    16      > FE_RESET_R                                       $18     !2, ->42
         17    > > FE_FETCH_R                                               $18, !3, ->42
   38    18    >   INIT_METHOD_CALL                                         !3, 'getElementsByTagName'
         19        SEND_VAL_EX                                              'img'
         20        DO_FCALL                                      0  $19     
         21        INIT_METHOD_CALL                                         $19, 'item'
         22        SEND_VAL_EX                                              0
         23        DO_FCALL                                      0  $20     
         24        ASSIGN                                                   !4, $20
   39    25      > JMPZ                                                     !4, ->41
   41    26    >   INIT_METHOD_CALL                                         !3, 'getAttribute'
         27        SEND_VAL_EX                                              'href'
         28        DO_FCALL                                      0  $22     
         29        ASSIGN                                                   !5, $22
   42    30        INIT_METHOD_CALL                                         !4, 'getAttribute'
         31        SEND_VAL_EX                                              'src'
         32        DO_FCALL                                      0  $24     
         33        ASSIGN                                                   !6, $24
   43    34        IS_EQUAL                                                 !5, !6
         35      > JMPZ                                                     ~26, ->41
   45    36    >   FETCH_OBJ_R                                      ~27     !3, 'parentNode'
         37        INIT_METHOD_CALL                                         ~27, 'replaceChild'
         38        SEND_VAR_EX                                              !4
         39        SEND_VAR_EX                                              !3
         40        DO_FCALL                                      0          
   36    41    > > JMP                                                      ->17
         42    >   FE_FREE                                                  $18
   52    43        INIT_METHOD_CALL                                         !1, 'saveHTML'
         44        DO_FCALL                                      0  $29     
         45        ECHO                                                     $29
         46      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.23 ms | 1390 KiB | 13 Q