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();
Output for git.master, git.master_jit, rfc.property-hooks
<body> <img src="https://i.stack.imgur.com/mOJ0a.png"> <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> </body>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
63.64 ms | 402 KiB | 8 Q