最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get video id from vimeo urlembed? - Stack Overflow

programmeradmin1浏览0评论

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\/(?: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.

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.\/(?: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.

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Jan 4, 2015 at 2:12 user3420034user3420034 5
  • Your regex works for the most part (with the exception of not capturing the param=test), not sure what the issue is? – l'L'l Commented Jan 4, 2015 at 2:21
  • What is the exact value of $videoLink? I tried your code with all of the urls shown on the previous example and it captures 11111111. Here, have a look: ideone./uTIhu4 – l'L'l Commented Jan 4, 2015 at 2:26
  • The regex should still work with that format; take a look at the example I linked. – l'L'l Commented Jan 4, 2015 at 2:56
  • Assuming you do basic input validation to make sure you actually have a vimeo url, wouldnt /(\d+)/ not do the trick perfectly fine? – stoppert Commented Jan 4, 2015 at 3:15
  • So just remove https?:\/\/(?:www\.)? — it should work. ( ideone./uTIhu4 ) – l'L'l Commented Jan 4, 2015 at 3:17
Add a ment  | 

3 Answers 3

Reset to default 4

The only answer that will always be correct is to ask Vimeo. Vimeo's oEmbed endpoint supports every Vimeo URL, and returns the video ID.

The oEmbed endpoint is kept up to date as the site evolves and adds more content, but these regexes will fall out of date.

In addition, they can not possible gather all urls. For example Vimeo allows a custom video url, which will not contain the id. The pattern (currently) is as follows:

https://vimeo./{user_url}/{video_url}

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./channels/mychannel/11111111';

if (preg_match("/https?:\/\/(?:www\.)?vimeo.\/(?: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.\/(?: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.

Try this one if you use short URLs from Vimeo:

$videoLink = 'http://vimeo./11111111'; // example

if (preg_match('#https?://vimeo./([0-9]+)#i', $videoLink, $match)) {
    $videoId = $match[1];
}
发布评论

评论列表(0)

  1. 暂无评论