1

Okay, here's a fun one.

I need to figure out what source a user is referencing when entering a copy and pasted Vimeo URL into an input box. For those that are unfamiliar, Vimeo currently has 4 different sources that are accessible through their simple API:

Videos belonging to a user

Valid URL structure: http://vimeo.com/user3825208 or https://vimeo.com/davekiss

Videos belonging to a group

Valid URL structure: http://vimeo.com/groups/eos550d or https://vimeo.com/groups/162

Videos belonging to a channel

Valid URL structure: http://vimeo.com/channels/hd or https://vimeo.com/channels/201

Videos belonging to an album

Valid URL structure: http://vimeo.com/album/1919683 or https://vimeo.com/album/mycustomname

So basically, I want to be able to run the URL into a function which will tell me what source the URL belongs to.

I've been using this for videos belonging to a user, but now I need to expand to all sources.

sscanf(parse_url($url, PHP_URL_PATH), '/%d', $video_id);

Maybe I should do this four times? preg_match('???', $url);

Thanks for your help!

Dave Kiss
  • 10,289
  • 11
  • 53
  • 75
  • Show what regex you currently have – zerkms Jul 03 '12 at 03:26
  • My approach has been for the user to tell me what they have been using in another dropdown box (group, album, channel, or username) but I want to simplify it. I'm pretty poor with regex, but I got `/^(http|https):\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/` working with single clips previously. – Dave Kiss Jul 03 '12 at 03:33

2 Answers2

3

You don't need regular expressions:

function discoverVimeo($url)
{
    if ((($url = parse_url($url)) !== false) && (preg_match('~vimeo[.]com$~', $url['host']) > 0))
    {
        $url = array_filter(explode('/', $url['path']), 'strlen');

        if (in_array($url[0], array('album', 'channels', 'groups')) !== true)
        {
            array_unshift($url, 'users');
        }

        return array('type' => rtrim(array_shift($url), 's'), 'id' => array_shift($url));
    }

    return false;
}

The following will return an array, with an index id and another index type, that will be one of:

  • user
  • album
  • channel
  • group
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • It'll take me a bit to test this, but I'll report back. Thanks for your time! – Dave Kiss Jul 03 '12 at 04:05
  • 2 things: I noticed the index of the $url to check should be `$url[1]` instead of `$url[0]`. Also, if the user omits the `http://` or `https://`, this fails. Is there any way to account for that as well? – Dave Kiss Jul 03 '12 at 18:30
  • @DaveKiss: Are you sure about the index? Did you try the latest version where I use `array_filter`? Because it should be 0... Regarding the protocol, you can do `$url = sprintf('http://%s', preg_replace('~^https?://~i', '', $url));` right before the `parse_url` call. – Alix Axel Jul 04 '12 at 00:51
  • @DaveKiss: Regarding the protocol, you can also use http://stackoverflow.com/a/2762083/89771. – Alix Axel Jul 04 '12 at 00:53
1

i would preg_match() the following regex patterns (in this order):

$channel_regex = '%vimeo\.com/channels/([a-zA-Z0-9]+)%/i';
$group_regex = '%vimeo\.com/groups/([a-zA-Z0-9]+)%/i';
$album_regex = '%vimeo\.com/album/([a-zA-Z0-9]+)%/i';
$user_regex = '%vimeo\.com/([a-zA-Z0-9]+)%/i';

this will regex match for:

vimeo.com/channels/...grab_this_data...
vimeo.com/groups/...grab_this_data...
vimeo.com/albums/...grab_this_data...

and if all of those preg_matches fail, (therefore a user URL), it will grab whatever is in the url:

vimeo.com/...grab_this_data...

good luck.

jay
  • 916
  • 1
  • 5
  • 13