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 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)