【问题标题】:Get URL from IFrame?从 IFrame 获取 URL?
【发布时间】:2012-06-19 23:29:25
【问题描述】:

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

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

问题在于 MTV。 URL 总是不同的,所以我想知道是否有办法获取 IFrame/嵌入代码的源值。

示例: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 本身而不是嵌入代码。

【问题讨论】:

标签: php iframe embed


【解决方案1】:

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

document.getElementById("myFrame").src

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 非常感谢您...完美!我还是个菜鸟,知道 SimpleXML 函数
    • 这对我不起作用。我只有iframe,没有div。所以我尝试了 $url = (string) $root->iframe['src'];似乎是个好主意...最终改用 DOMDocument。
    猜你喜欢
    • 2011-04-30
    • 2010-10-30
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多