DOMDocument() 获取整个标签,而不仅仅是值


DOMDocument() get whole Tag and not only value

我使用 DOMDocument() 来解析 html 并获取 iframe:

$content  = '<p>Lorem ip.., </p>
<iframe width="500" height="281" src="http://www.youtube.com/embed/Fv6sHgq6hFc?feature=oembed" frameborder="0" allowfullscreen=""></iframe>
 <p>sed diam volup...orem ipsum dolor sit amet.</p>
            ';
$document = new DOMDocument();
$document->loadHTML( $content );            
$lst = $document->getElementsByTagName('iframe');
$videos = array();
for ( $i = 0; $i < $lst->length; $i++ ) {
 $iframe = $lst->item($i);
 var_dump($iframe);
 /* I want to get "<iframe width="500" height="281" src="http://www.youtube.com/embed/Fv6sHgq6hFc?feature=oembed" frameborder="0" allowfullscreen=""></iframe>" here */
}

如何获得像<iframe src="..." attribute 1><iframe src="..." attribute 1></iframe>一样的整个 iframe?

谢谢!

尝试:

foreach($lst as $iframe) {
    var_dump($document->saveHtml($iframe));
}

仅适用于 PHP>= 5.3.6

否则:

foreach($lst as $iframe) {
    $cloned = $iframe->cloneNode(TRUE);
    $newdoc = new DOMDocument();
    $newdoc->appendChild($newdoc->importNode($cloned,TRUE));
    var_dump($newdoc->saveHTML());
}
相关文章: