php dom add an item

2

Hello friends, you could help me with the following.

I have that HTML code:

<figure class="class"><iframe src="src" width="xxx" height="xxx"></iframe></figure>

But I want to add a iframe more so it looks like this:

<figure class="class"><iframe><iframe src="src" width="xxx" height="xxx"></iframe><iframe></figure>

This is the code I'm using to wrap it in those tags but I can not add the iframe I'm missing.

$iframes = $doc->getElementsByTagName('iframe');
    foreach ($iframes as $iframeViejo) {
        //Crear un nuevo iframe y asignar el src
        $iframeMainn = $doc->createElement('iframe');
        $iframeNuevo->setAttribute('src', $iframeViejo->getAttribute('src'));
        $iframeNuevo->setAttribute('width','560');
        $iframeNuevo->setAttribute('height','615');
        //Crear un nuevo figure y agregarle el nuevo iframe
        $figureNuevo = $doc->createElement('figure');
        $figureNuevo->setAttribute('class','op-interactive');
        $figureNuevo->appendChild($iframeNuevo);
        //reemplazar viejo por nuevo
        $iframeViejo->parentNode->replaceChild($figureNuevo, $iframeViejo);
    }

Could you help me please,

greetings

    
asked by skycomputer2 27.02.2017 в 23:35
source

1 answer

0

The code you share:

  • You have an error with the names of the variables: you create a variable iframeMainn that is not used later, and you try to use iframeNuevo without having initialized it. I guess really where it puts iframeMainn should put iframeNuevo .
  • Instead of adding a new iframe add a new figure : so the result will be figure > figure > iframe instead of figure > iframe > iframe .
  • Use DOMDocument::getElementsByTagName . This is not a problem in itself, but it is important to remember that this method returns a dynamic list, that is, if you add another iframe to the DOM within your foreach loop, a new element will be added to the list and you will end up inside of an infinite loop.

With that in mind and trying to change the code you share as little as possible, I would suggest you make the following changes:

  • Change iframeMainn by iframeNuevo :

    $iframeNuevo = $doc->createElement('iframe');
    
  • Add a iframe instead of a figure :

    $figureNuevo = $doc->createElement('iframe');
    
  • Now that you are adding a new iframe you would have the problem of the infinite loop that I commented above; to avoid this, return the data of getElementsByTagName to a normal array and then go through that array. The new array will be "static" since it will only contain the original elements of getElementsByTagName :

    $iframes = $doc->getElementsByTagName('iframe');
    $arrayiframes = array();
    foreach ($iframes as $iframeViejo) {
        $arrayiframes[] = $iframeViejo;
    }
    
    foreach($arrayiframes as $iframeViejo) {
        ....
    
  • With those changes the code would look like this:

    <?php
    
    $doc = new DOMDocument();
    @$doc->loadHTML("<figure class='class'><iframe src='src' width='300' height='200'></iframe></figure>");
    
    $iframes = $doc->getElementsByTagName('iframe');
    $arrayiframes = array();
    foreach ($iframes as $iframeViejo) {
        $arrayiframes[] = $iframeViejo;
    }
    
    foreach($arrayiframes as $iframeViejo) {
        //Crear un nuevo iframe y asignar el src
        $iframeNuevo = $doc->createElement('iframe');
        $iframeNuevo->setAttribute('src', $iframeViejo->getAttribute('src'));
        $iframeNuevo->setAttribute('width','560');
        $iframeNuevo->setAttribute('height','615');
        //Crear un nuevo figure y agregarle el nuevo iframe
        $figureNuevo = $doc->createElement('iframe');
        $figureNuevo->appendChild($iframeNuevo);
        //reemplazar viejo por nuevo
        $iframeViejo->parentNode->replaceChild($figureNuevo, $iframeViejo);
    }
    
    echo $doc->saveHTML();
    

    With which the expected result is obtained:

    <figure class="class"><iframe><iframe src="src" width="560" height="615"></iframe></iframe></figure>
    

    Note: to see the result you should see the source code and not inspect the code. This is due to how the browsers represent iframe s (what is between the <iframe></iframe> tags will only be displayed if the browser does not support iframe .

        
    answered by 28.02.2017 в 13:29