DOMDocument()获取整个Tag而不仅仅是值

时间:2014-11-30 16:08:49

标签: php dom domdocument

我使用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?

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试:

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

仅适用于PHP&gt; = 5.3.6

否则:

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