2014-09-22 72 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 

    } 
?>