从IFrame获取网址?

时间:2012-06-17 22:36:55

标签: php iframe embed

我想让用户能够添加视频,但我的其中一个网站存在问题。

我将允许来自Youtube,Vimeo,Vevo,Daily Motion和MTV的视频。

问题出在MTV上。 URL始终不同,所以我想知道是否有办法获取IFrame / embed代码的源值。

示例:MTV嵌入代码为:

<div style="background-color:#000000;width:520px;">
<div style="padding:4px;">
<iframe src="http://media.mtvnservices.com/embed/mgid:uma:videolist:mtv.com:1687162/cp~instance%3Dfullepisode%26autoPlay%3Dfalse%26series%3D2211%26seriesId%3D29240%26channelId%3D1%26id%3D1687162%26instance%3Dfullepisode%26uri%3Dmgid%3Auma%3Avideolist%3Amtv.com%3A1687162" width="512" height="288" frameborder="0"></iframe>
<p style="text-align:left;background-color:#FFFFFF;padding:4px;margin-top:4px;margin-bottom:0px;font-family:Arial, Helvetica, sans-serif;font-size:12px;">Get More: 
    <a href="http://www.mtv.com/shows/teen_mom/season_4/series.jhtml" style="color:#439CD8;" target="_blank">Teen Mom (Season 4)</a>, <a href="http://www.mtv.com/videos/home.jhtml" style="color:#439CD8;" target="_blank">Full Episodes</a></p></div></div>

有没有办法可以从完整的字符串中获取IFrame源值?

我只想:

http://media.mtvnservices.com/embed/mgid:uma:videolist:mtv.com:1687162/cp~instance%3Dfullepisode%26autoPlay%3Dfalse%26series%3D2211%26seriesId%3D29240%26channelId%3D1%26id%3D1687162%26instance%3Dfullepisode%26uri%3Dmgid%3Auma%3Avideolist%3Amtv.com%3A1687162

任何帮助将不胜感激。其他视频源很简单,因为我能够使用物理URL本身而不是嵌入代码。

2 个答案:

答案 0 :(得分:2)

这是有效的HTML,因此您可以使用SimpleXML轻松解析它:

$root = simplexml_load_string($embed_code);
$url = (string) $root->div->iframe['src'];

答案 1 :(得分:1)

要做到这一点,这很容易,你需要的就是这一行代码。

document.getElementById("myFrame").src