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.
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.