Php 如何从具有多个iframe的字符串中获取第一个iframe

Php 如何从具有多个iframe的字符串中获取第一个iframe,php,Php,如何从带有几个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.youtub

如何从带有几个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>';
$string=
'
';

可以使用explode功能完成吗

$tag = explode("</iframe>",$string);
$first = $tag[0]."</iframe>";
$tag=explode(“,$string);
$first=$tag[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

  }
?>