2014-09-22 74 views
0

如何通过多个iframe标记从字符串获取第一个iframe?字符串只包含一个或多个iframe标签。 例如:如何从字符串中获得第一个iframe iframe iframe

$string = 
'<iframe width="560" height="315" src="http://www.youtube.com/embed/" frameborder="0" allowfullscreen></iframe> 
<iframe width="560" height="315" src="http://www.youtube.com/embed/?" frameborder="0" allowfullscreen></iframe>'; 

回答

0

它可以使用爆炸功能吗?

$tag = explode("</iframe>",$string); 
$first = $tag[0]."</iframe>"; 

也许财产以后这样的

+0

简单,效果很好,谢谢 – Lukas 2014-09-22 10:01:33

0

我建议是这样的:

<?PHP 
    $htmlinput = <<<EOT 
<iframe width="560" height="315" src="http://www.youtube.com/embed/" frameborder="0" allowfullscreen></iframe> 
<iframe width="560" height="315" src="http://www.youtube.com/embed/?" frameborder="0" allowfullscreen></iframe> 
EOT; 
?> 

<?PHP 
    $doc = new DOMDocument(); 
    $doc->loadHTML($htmlinput); 

    $arr = $doc->getElementsByTagName("iframe"); // DOMNodeList Object 
    foreach($arr as $item) { // DOMElement Object 

    } 
?>