PHP DOM wrap blockquote between iframe tags

0

could you help me I have this html code:

<div class="media_embed">
    <blockquote class="twitter-video" data-lang="es" height="" width="">
        <p dir="ltr" lang="es">"Mi cabeza está en REGRESAR a jugar y TRIUNFAR”<br />
        <br />
        ¡<a href="https://twitter.com/MartinZB85?ref_src=twsrc%5Etfw">@MartinZB85</a> reconoce que le faltó madurez durante su paso por <a href="https://twitter.com/ClubAmerica?ref_src=twsrc%5Etfw">@ClubAmerica</a>!<br />
        <br />
        | <a href="https://twitter.com/r_umanzor?ref_src=twsrc%5Etfw">@r_umanzor</a><br />
        <br />
        | <a href="https://twitter.com/DANGAMEZ?ref_src=twsrc%5Etfw">@DANGAMEZ</a> y <a href="https://twitter.com/ElDonLalonio?ref_src=twsrc%5Etfw">@ElDonLalonio</a><br />
        <br />
        <a href="https://t.co/0aF1jxNOTB">https://t.co/0aF1jxNOTB</a> <a href="https://t.co/a7AnKbJURk">pic.twitter.com/a7AnKbJURk</a></p>
        — DIARIO RÉCORD (@record_mexico) <a href="https://twitter.com/record_mexico/status/954051794048569345?ref_src=twsrc%5Etfw">18 de enero de 2018</a>
    </blockquote>
    <script async="" charset="utf-8" height="" src="https://platform.twitter.com/widgets.js" width=""></script>
</div>

Which has to have this output:

<div class="media_embed">
    <figure class="op-interactive">
        <iframe>
            <blockquote class="twitter-video" data-lang="es" height="" width="">
                <p dir="ltr" lang="es">"Mi cabeza está en REGRESAR a jugar y TRIUNFAR”<br />
                <br />
                ¡<a href="https://twitter.com/MartinZB85?ref_src=twsrc%5Etfw">@MartinZB85</a> reconoce que le faltó madurez durante su paso por <a href="https://twitter.com/ClubAmerica?ref_src=twsrc%5Etfw">@ClubAmerica</a>!<br />
                <br />
                | <a href="https://twitter.com/r_umanzor?ref_src=twsrc%5Etfw">@r_umanzor</a><br />
                <br />
                | <a href="https://twitter.com/DANGAMEZ?ref_src=twsrc%5Etfw">@DANGAMEZ</a> y <a href="https://twitter.com/ElDonLalonio?ref_src=twsrc%5Etfw">@ElDonLalonio</a><br />
                <br />
                <a href="https://t.co/0aF1jxNOTB">https://t.co/0aF1jxNOTB</a> <a href="https://t.co/a7AnKbJURk">pic.twitter.com/a7AnKbJURk</a></p>
                — DIARIO RÉCORD (@record_mexico) <a href="https://twitter.com/record_mexico/status/954051794048569345?ref_src=twsrc%5Etfw">18 de enero de 2018</a>
            </blockquote>
        </iframe>
    </figure>
    <script async="" charset="utf-8" height="" src="https://platform.twitter.com/widgets.js" width=""></script>
</div>

I have this code in which I use PHP DOM but I can not get it:

$htmlEncoded = mb_convert_encoding($body1, 'HTML-ENTITIES', 'UTF-8');
$doc = new DOMDocument;
$opcionesLibXML = LIBXML_COMPACT | LIBXML_HTML_NODEFDTD;
libxml_use_internal_errors(true);
@$doc->loadHTML($htmlEncoded, $opcionesLibXML);
libxml_use_internal_errors(false);
    $scriptstag = $doc->getElementsByTagName('blockquote');
    foreach ($scriptstag as $scripts) {
        $src= $scripts->getAttribute('class');
        $posgif = strpos($src, 'twitter-video');
        if ($posgif === false){
        }else{
            $figureNuevo = $doc->createElement('figure');
            $figureNuevo->setAttribute('class','op-interactive');
            $iframeNuevo = $doc->createElement('iframe');
            $figureNuevo->appendChild($iframeNuevo);
            $scripts->parentNode->replaceChild($figureNuevo, $scriptstag);
        }
    }                           
$body1 = $doc->saveHTML($doc->documentElement);
?>

The output returns it to me like this:

<div class="media_embed">
    <figure class="op-interactive">
        <iframe>
        </iframe>
    </figure>
    <script async="" charset="utf-8" height="" src="https://platform.twitter.com/widgets.js" width=""></script>
</div>

Could you help me please? Greetings

    
asked by skycomputer2 20.01.2018 в 01:10
source

1 answer

1

The error is because you are replacing blockquote ( eg: $scripts->parentNode->replaceChild($figureNuevo, $scriptstag); ), instead of adding it to iframe .

Solution:

  • You can use parentNode to refer to DOMElement parent of blockquote and from this call to insertBefore to add the figure exactly before blockquote .
  • You can use [ appendChild ] over $iframeNuevo to add% blockquote within this.

Example:

$htmlEncoded = mb_convert_encoding($body1, 'HTML-ENTITIES', 'UTF-8');
$doc = new DOMDocument();
$opcionesLibXML = LIBXML_COMPACT | LIBXML_HTML_NODEFDTD;

libxml_use_internal_errors(true);
@$doc->loadHTML($htmlEncoded, $opcionesLibXML);
libxml_use_internal_errors(false);
$scriptstag = $doc->getElementsByTagName('blockquote');

foreach ($scriptstag as $scripts) {
    $src= $scripts->getAttribute('class');
    if (strpos($src, 'twitter-video') === false) {
        continue;
    }

    $figureNuevo = $doc->createElement('figure');
    $figureNuevo->setAttribute('class','op-interactive');

    $iframeNuevo = $doc->createElement('iframe');
    $figureNuevo->appendChild($iframeNuevo);

    // Agregamos el 'figure' antes del 'blockquote'
    $scripts->parentNode->insertBefore($figureNuevo, $scripts);

    // Agregamos el 'blockquote' dentro del 'iframe'
    $iframeNuevo->appendChild($scripts);
}                           
$body1 = $doc->saveHTML($doc->documentElement);

Demo

    
answered by 20.01.2018 / 12:08
source