dongza5150 2014-06-26 05:35
浏览 50
已采纳

如何使用PHP从嵌入URL获取视频ID? [重复]

This question already has an answer here:

I am having an youtube embed URL like this

<object width="420" height="315">
<param name="movie" value="//www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="//www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true">
</embed></object>

Now i want to get ID of video Using PHP. Please someone help

</div>
  • 写回答

2条回答 默认 最新

  • douyi0902 2014-06-26 06:13
    关注

    You could use something like this to get the ID of the youtube link:

    $your_url='http://www.youtube.com/watch?var1=blabla#v=GvJehZx3eQ1$var2=bla';
    
    function get_youtube_id_from_url($url){
        if (stristr($url,'youtu.be/'))
            { preg_match('/(https:|http:|)(\/\/www\.|\/\/|)(.*?)\/(.{11})/i', $url, $final_ID); return $final_ID[4]; }
        else 
            { preg_match('/(https:|http:|):(\/\/www\.|\/\/|)(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-z_A-Z0-9]{11})/i', $url, $IDD); return $IDD[6]; }
    }
    
    echo get_youtube_id_from_url($your_url);
    

    Output:

    GvJehZx3eQ1
    

    And something like this to get the youtube link from the embed tags:

    $sLink = "<embed src=\"www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US\"></embed>";
    preg_match('/< *embed[^>]*src *= *["\']?([^"\']*)/i', $sLink,$aMatch);
    echo $aMatch[1];
    

    Output:

    www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US
    

    You will have to use a little bit of your logic to make both these functions work together :) But this is a good starting point!

    Bear in mind that the " will need to be escaped with a \ if you are enclosing the expression in double quotes.

    P.S: Got the first function from somewhere, just don't remember where.

    EDIT

    You could also use Javascript for this:

    var re = /(\?v=|\/\d\/|\/embed\/|\/v\/|\.be\/)([a-zA-Z0-9\-\_]+)/;
    var urlArr = [
        "http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index",
        "http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o",              
    ];
    for (var i = 0; i < urlArr.length; i++) {
        alert(urlArr[i].match(re)[2]);
    }
    

    JSFiddle: http://jsfiddle.net/A7LYZ/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本