1

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.

Community
  • 1
  • 1
  • 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 Jan 04 '15 at 02: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: http://ideone.com/uTIhu4 – l'L'l Jan 04 '15 at 02:26
  • The regex should still work with that format; take a look at the example I linked. – l'L'l Jan 04 '15 at 02: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 Jan 04 '15 at 03:15
  • So just remove `https?:\/\/(?:www\.)?` — it should work. ( http://ideone.com/uTIhu4 ) – l'L'l Jan 04 '15 at 03:17

3 Answers3

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.com/{user_url}/{video_url}

Dashron
  • 3,968
  • 2
  • 14
  • 21
3

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.

Alex
  • 744
  • 8
  • 20
0

Try this one if you use short URLs from Vimeo:

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

if (preg_match('#https?://vimeo.com/([0-9]+)#i', $videoLink, $match)) {
    $videoId = $match[1];
}
Tomasz Racia
  • 1,786
  • 2
  • 15
  • 23