PHP DOMDocument parentNode->replaceChild 导致 foreach 跳过下一项


PHP DOMDocument parentNode->replaceChild causing foreach to skip next item

我正在使用 DOMDocument 解析 $content 变量中的 html,以将所有 iframe 替换为图像。 foreach 只是取代了 ODD iframe。 我已经删除了foreach中的所有代码,并发现导致此问题的代码段是:"$iframe->parentNode->replaceChild($link,$iframe);"

为什么 foreach 会跳过所有奇怪的 iframe?

代码:

        $count = 1;
        $dom = new DOMDocument;
        $dom->loadHTML($content);
        $iframes = $dom->getElementsByTagName('iframe');
        foreach ($iframes as $iframe) {
            $src = $iframe->getAttribute('src');
            $width = $iframe->getAttribute('width');
            $height = $iframe->getAttribute('height');
            $link = $dom->createElement('img');
            $link->setAttribute('class', 'iframe-'.self::return_video_type($iframe->getAttribute('src')).' iframe-'.$count.' iframe-ondemand-placeholderImg');
            $link->setAttribute('src', $placeholder_image);
            $link->setAttribute('height', $height);
            $link->setAttribute('width', $width);
            $link->setAttribute('data-iframe-src', $src);
            $iframe->parentNode->replaceChild($link, $iframe);
            echo "here:".$count;
            $count++;
        }
        $content = $dom->saveHTML();
        return $content;

这是问题代码行

        $iframe->parentNode->replaceChild($link, $iframe);

一个 DOMNodeList,例如从 getElementsByTagName 返回的 ,是 "live":

也就是说,对底层文档结构的更改会反映在所有相关的 NodeList 中......对象

因此,当您删除元素(在本例中为用另一个元素替换它)时,它不再存在于节点列表中,并且下一个行中的元素将占据其在索引中的位置。然后,当foreach命中下一个迭代,从而命中下一个索引时,将有效地跳过一个索引。

不要像这样通过foreach从 DOM 中删除元素。


一种有效的方法是使用 while 循环进行迭代和替换,直到$iframes节点列表为空。

例:

while ($iframes->length) {
    $iframe = $iframes->item(0);
    $src = $iframe->getAttribute('src');
    $width = $iframe->getAttribute('width');
    $height = $iframe->getAttribute('height');
    $link = $dom->createElement('img');
    $link->setAttribute('class', 'iframe-'.self::return_video_type($iframe->getAttribute('src')).' iframe-'.$count.' iframe-ondemand-placeholderImg');
    $link->setAttribute('src', $placeholder_image);
    $link->setAttribute('height', $height);
    $link->setAttribute('width', $width);
    $link->setAttribute('data-iframe-src', $src);
    $iframe->parentNode->replaceChild($link, $iframe);
    echo "here:".$count;
    $count++;
}

今天面对这个问题,并以答案为指导,我为你们制作一个简单的代码解决方案

$iframes = $dom->getElementsByTagName('iframe');
for ($i=0; $i< $iframes->length; $i++) {
    $iframe = $iframes->item($i);
    if("condition to replace"){
        // do some replace thing
        $i--;
    }
}

希望这有帮助。