php dom替换第一个元素,第二个不替换,第三个替换

时间:2019-05-22 21:13:31

标签: php dom

im使用php dom替换了一些html标记,但我不知道为什么不替换所有元素,只是第一和第三,

这是我的代码:

          $valFieldBody ='<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod</p><div class="media_embed"><div height="" style="width:100%;height:0;padding-bottom:56%;position:relative;" width=""><iframe allowfullscreen="" class="giphy-embed" frameborder="0" height="100%" src="https://giphy.com/embed/9GIiC03mgON4ZsZVdX" style="position:absolute" width="100%"/></div></div><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corrupti necessitatibus</p><div class="media_embed"><div height="" style="width:100%;height:0;padding-bottom:56%;position:relative;" width=""><iframe allowfullscreen="" class="giphy-embed" frameborder="0" height="100%" src="https://giphy.com/embed/V2V3Q1Yn1rpIs" style="position:absolute" width="100%"/></div></div><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p><div class="media_embed"><div height="" style="width:100%;height:0;padding-bottom:70%;position:relative;" width=""><iframe allowfullscreen="" class="giphy-embed" frameborder="0" height="100%" src="https://giphy.com/embed/2yuS0kxvPq7FJxEYl1" style="position:absolute" width="100%"/></div></div>';
          $htmlEncoded = mb_convert_encoding($valFieldBody, 'HTML-ENTITIES', 'UTF-8');
          $doc = new DOMDocument;
          $opcionesLibXML = LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD;
          libxml_use_internal_errors(true);
          @$doc->loadHTML($htmlEncoded, $opcionesLibXML);
          libxml_use_internal_errors(false);
          $iframe_tag = $doc->getElementsByTagName('iframe');
          foreach ($iframe_tag as $key => $iframe_items){
                $src_iframe = $iframe_items->getAttribute("src");
                $imgNuevo = $doc->createElement('img');
                $imgNuevo->setAttribute('src', $src_iframe."embed");
                $iframe_items->parentNode->replaceChild($imgNuevo, $iframe_items);
          }
          $valFieldBody = $doc->saveHTML($doc->documentElement);

输出是这样的

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
<div class='media_embed'>
  <div height='' style='width:100%;height:0;padding-bottom:56%;position:relative;' width=''>
        <img src='https://giphy.com/embed/9GIiC03mgON4ZsZVdXembed'>
  </div>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corrupti necessitatibus</p>
<div class='media_embed'>
    <div height='' style='width:100%;height:0;padding-bottom:56%;position:relative;' width=''>
        <iframe allowfullscreen='' class='giphy-embed' frameborder='0' height='100%' src='https://giphy.com/embed/V2V3Q1Yn1rpIs' style='position:absolute' width='100%'></iframe>
  </div>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
<div class='media_embed'>
    <div height='' style='width:100%;height:0;padding-bottom:70%;position:relative;' width=''>
        <img src='https://giphy.com/embed/2yuS0kxvPq7FJxEYl1embed'>
    </div>
</div>

没有替换第二个iframe,但我不知道为什么, 你能帮我吗?

0 个答案:

没有答案