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


how can I get first iframe from string with many iframes

如何从带有多个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>';

可以使用爆炸函数吗?

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

也许有类似的东西

我建议这样做:

<?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
  }
?>