get id video vimeo with regexp preg_match
Asked Answered
L

3

5

I try to get the id video from an url of vimeo, but looking in the source code sometimes it comes like:

http://vimeo.com/XXXXXXXXX

and others like:

http://player.vimeo.com/video/XXXXXXXXX

I just get the id video, but it must be with regExp because I must parse a content from a blog and format like a facebook page does when we insert code html.

This is the regExp that I made:

/vimeo\.com\/(\w+\s*\/?)*([0-9]+)*$/i

Can you help me? I appreciate all your help.

PD: Sorry for my english

Lonnalonnard answered 17/6, 2013 at 20:30 Comment(2)
why do you have \s* in there? There shouldn't be any spaces in a URL. Also, what is the format of the XXXXXXX part? Is it always just numbers and only numbers?Kadiyevka
Hi @Dgrin91, I put \s* there only for testing. The XXXXXXXX part I see it is only numbers.Lonnalonnard
G
25

Why not ask Vimeo about the video?

http://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/17892962

The above URL will give you a nice JSON response with lots of useful information, including the video ID and appropriate HTML to embed the video in a page. Just pass your URLs to the oEmbed endpoint as shown above.

Example JSON response (truncated long lines for display purposes)

{
    "type": "video",
    "version": "1.0",
    "provider_name": "Vimeo",
    "provider_url": "http://vimeo.com/",
    "title": "Danny MacAskill - \"Way Back Home\"",
    "author_name": "Dave Sowerby",
    "author_url": "http://vimeo.com/tdave",
    "is_plus": "1",
    "html": "<iframe src=\"http://player.vimeo.com/video/17892962\" …",
    "width": 1280,
    "height": 720,
    "duration": 462,
    "description": "A journey from Edinburgh to Skye where Danny finds …",
    "thumbnail_url": "http://b.vimeocdn.com/ts/399/121/399121670_1280.jpg",
    "thumbnail_width": 1280,
    "thumbnail_height": 720,
    "video_id": 17892962
}

For full information, see their oEmbed documentation.

Gamine answered 17/6, 2013 at 21:4 Comment(5)
Excellent resource @salathe. That's will be my next question. I must show information of de video, like description, author, etc.Lonnalonnard
Let's hope someone finds this answer when you ask. :)Gamine
This is vimeo's preferred method of turning a url info a video id. There are many different urls a video can have (within an album, within a group, within a channel, with a custom url string) and there will surely be more in the futureQuintilla
@Gamine is it possible that we can directly get the video thumbnail. without any request.Like this http://img.youtube.com/vi/HwP5NG-3e8I/2.jpg.Quamash
You rock! Simple and appears to work perfectlySubsume
L
3

http://(player\.)?vimeo\.com(/video)?/(\d+) covers both of your examples, though I'm sure it could be optimised given further URLs.

Larner answered 17/6, 2013 at 20:34 Comment(2)
thanks @Joel-Hinz. I try to put this regExp but it didn't work yet. Could you send me or publish some test code. I'm not good for regExpLonnalonnard
@AnbalMauricioPachecoGonzlez You need to use different delimiters, try preg_match_all('#https?://(player\.)?vimeo\.com(/video)?/(\d+)#i', $string, $m);Whilom
K
0

This should work: vimeo\.com/(\w*/)*(\d+). The (\w/)* lets you have a URL like vimeo.com/video/something/something-more/more-more-more/XXXXXXXXX without any problems. Then you can grab the XXXXXXXX by just getting the second ground (the \d+). If you know how many digits the video id has you can replace \d+ with \d{n} where n is the number of digits.

Kadiyevka answered 17/6, 2013 at 20:43 Comment(5)
Thanks @Dgrin91 for your answer. I execute this: "@vimeo\.com/(\w/)*(\d+)@i" like pattern and "player.vimeo.com/video/68400837" like subject. But the regExp didn't found matches. I don't see any issue in your code. Could you help me???Lonnalonnard
@AnbalMauricioPachecoGonzlez You are correct, there is something wrong in my regex, it seems to be a problem with the '/' after the \w. Ill try and fix it now, one minute.Kadiyevka
@AnbalMauricioPachecoGonzlez Ok, I figured out the original problem. It should have been (\w*/)*. That is your best regex, and it works for your cases. Ill update my answer now.Kadiyevka
This will not work for custom urls such as vimeo.com/dashron/companioncube. All users can create urls like this via their advanced video settings. You should use the oembed method mentioned below.Quintilla
@Quintilla Yes, my regex was just meant for the input patterns the OP provided. Im not familiar with Vimeo, so I dont know all the possible patterns. Your solution sounds good though.Kadiyevka

© 2022 - 2024 — McMap. All rights reserved.