user998163
user998163

Reputation: 483

DOMDocument() get whole Tag and not only value

I use DOMDocument() to parse html and get the iframes:

$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 */
}

How do I get the whole iframe like <iframe src="..." attribute 1> or <iframe src="..." attribute 1></iframe> ?

Thank You!

Upvotes: 0

Views: 294

Answers (1)

M. Page
M. Page

Reputation: 2814

Try:

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

Works only with PHP >= 5.3.6

Otherwise:

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

Upvotes: 1

Related Questions