我正在使用html DOMDocument在$content
变量中找到所有iFrames实例。我能够为每个实例输出一个图像,但宁愿用图像替换iframe,然后保存回内容变量。我希望替换当前的iframe而不是echo
我的结果。我该怎么做?
$count = 1;
$dom = new DOMDocument;
$dom->loadHTML($content);
$iframes = $dom->getElementsByTagName('iframe');
foreach ($iframes as $iframe) {
echo "<img class='iframe-".self::return_video_type($iframe->getAttribute('src'))." iframe-ondemand-placeholderImg iframe-".$count."' src='" .$placeholder_image. "' height='".$iframe->getAttribute('height')."' width='" .$iframe->getAttribute('width'). "' data-iframe-src='" .$iframe->getAttribute('src'). "' /><br />";
$count++;
}
$content = $dom->saveHTML();
return $content;
答案 0 :(得分:1)
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
http://php.net/manual/en/domnode.replacechild.php
这样的事情:
$iframes = $dom->getElementsByTagName('iframe');
$i = $iframes->length - 1;
while ($i > -1) {
$iframe = $iframes->item($i);
$ignore = false;
$img = $dom->createElement("img");
$img->setAttribute("src",$iframe->getAttribute('src'));
$iframe->parentNode->replaceChild($img, $iframe);
$i--;
}