dqqvravff05370501 2015-01-04 02:12
浏览 146
已采纳

如何从vimeo url / embed获取视频ID?

So I'm trying to get the video id from a vimeo url using a regex.

Based on this: Get video id from Vimeo url

The following regex should do the trick:

if (preg_match("/https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/", $videoLink, $id)) {
        $videoId = $id[3];
    }

But it isn't working, and I can't for the life of me work out why.

Is there a difference between regex in Javascript and PHP/am I misusing preg_match? I've looked around on Stack Overflow for a while and just can't find a working regex for getting the video id from a vimeo url/embed.

There are plenty of regex out there for it, but none show their implementation.

  • 写回答

3条回答 默认 最新

  • doubi6898 2015-01-04 02:34
    关注

    Your code does not seem to contain any errors. Did you check the input and output variables $videoLink and $id with print_r($videoLink) and print_r($id)? Maybe this gives you an advice.

    Here an working example using your code:

    $videoLink = 'https://vimeo.com/channels/mychannel/11111111';
    
    if (preg_match("/https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/", $videoLink, $id)) {
        $videoId = $id[3];
    }
    
    print_r($id); // the array
    print_r($videoId); // 11111111
    

    If print_r() does not help: Do you get any errors, if showing them is activated?

    Edit:

    If you want the http(s):// to be optional, you can use the following code:

    if (preg_match("/(?:https?:\/\/)?(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/", $videoLink, $id)) {
        $videoId = $id[3];
    }
    

    Wrapping it with (...)? makes it optional and the ?: excludes it from the result.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘