Reputation: 10487
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:
Valid URL structure: http://vimeo.com/user3825208 or https://vimeo.com/davekiss
Valid URL structure: http://vimeo.com/groups/eos550d or https://vimeo.com/groups/162
Valid URL structure: http://vimeo.com/channels/hd or https://vimeo.com/channels/201
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!
Upvotes: 1
Views: 921
Reputation: 154513
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
Upvotes: 3
Reputation: 916
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.
Upvotes: 1