【发布时间】:2015-01-23 22:32:21
【问题描述】:
我正在使用 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);
【问题讨论】:
标签: php iframe foreach html-parser