Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php DOMDocument()获取整个标记,而不仅仅是值_Php_Dom_Domdocument - Fatal编程技术网

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

Php DOMDocument()获取整个标记,而不仅仅是值,php,dom,domdocument,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 v

我使用
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 */
}
$content=”Lorem ip

这是一个很好的例子

'; $document=新的DOMDocument(); $document->loadHTML($content); $lst=$document->getElementsByTagName('iframe'); $videos=array(); 对于($i=0;$i<$lst->length;$i++){ $iframe=$lst->item($i); 变量转储($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());
}

你想要什么还不清楚。请给我们一个带有iframe的HTML示例,并告诉我们您希望在其中获得什么。@M.Page请查看我的编辑。@M.Page我希望获得整个iframe标记,目前我只能输出其属性。我这样做了,但出现了此错误:`Warning:DOMDocument::saveHTML()需要精确的0个参数,1在`yesp'中给出。刚刚检查过。5.3.3. 还有其他方法吗?我做了:)并得到:“致命错误:对非对象I调用成员函数appendChild()”此外,DOMNodeList实现了可遍历性。它可以与foreach()一起使用
foreach($lst作为$iframe){…}