fork(5) download
  1. <?php //http://es.stackoverflow.com/q/44617/127
  2.  
  3. $html = '
  4. <body>
  5. <iframe allowfullscreen="" frameborder="0" height="315px"
  6. src="https://w...content-available-to-author-only...e.com/embed/xxxyyyxxx" width="560px">
  7. </iframe>
  8. <div><p>¡Acá más texto!</p></div>
  9. <iframe src="https://w...content-available-to-author-only...o.com/iframe" width="333px"></iframe>
  10. </body>';
  11.  
  12. //Convertir utf8 a entities
  13. $htmlISO = mb_convert_encoding($html, 'ISO-8859-1');
  14.  
  15.  
  16. //Generar el DOM
  17. $doc = new DOMDocument;
  18. $opcionesLibXML = LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD;
  19. $doc->loadHTML($htmlISO, $opcionesLibXML);
  20.  
  21.  
  22. //Obtener todos los iframes
  23. $iframes = $doc->getElementsByTagName('iframe');
  24.  
  25. //Reemplazar c/u
  26. foreach ($iframes as $iframeViejo) {
  27. //Crear un nuevo iframe y asignar el src
  28. $iframeNuevo = $doc->createElement('iframe');
  29. $iframeNuevo->setAttribute('src', $iframeViejo->getAttribute('src'));
  30.  
  31. //Crear un nuevo figure y agregarle el nuevo iframe
  32. $figureNuevo = $doc->createElement('figure');
  33. $figureNuevo->appendChild($iframeNuevo);
  34.  
  35. //reemplazar viejo por nuevo
  36. $iframeViejo->parentNode->replaceChild($figureNuevo, $iframeViejo);
  37. }
  38.  
  39.  
  40. //DOM -> string final
  41. $resultado = $doc->saveHTML($doc->documentElement);
  42. //documentElement :: importante para no recibir entities
  43. echo $resultado;
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
<body>
             <figure><iframe src="https://w...content-available-to-author-only...e.com/embed/xxxyyyxxx"></iframe></figure>
             <div><p>¡Acá más texto!</p></div>
             <figure><iframe src="https://w...content-available-to-author-only...o.com/iframe"></iframe></figure>
         </body>