Skip to content

Instantly share code, notes, and snippets.

@lutzissler
Last active August 29, 2015 14:08
Show Gist options
  • Save lutzissler/8867469804c3e563578c to your computer and use it in GitHub Desktop.
Save lutzissler/8867469804c3e563578c to your computer and use it in GitHub Desktop.
Get media info object based on media URL. Currently support YouTube, Vimeo and Soundcloud.
function getMediaInfo($mediaUrl) {
$mediaUrl = trim($mediaUrl);
// Check for YouTube
$matchFound = preg_match('|^\s*https?://www.youtube.com/watch\?v=([a-zA-Z0-9\.-]+)(#.*)?\s*$|', $mediaUrl, $matches);
if ($matchFound) {
return (object)array(
'mediaUrl' => $mediaUrl,
'mediaSource' => 'youtube',
'mediaID' => $matches[1],
);
}
// Check for Vimeo
$matchFound = preg_match('|^\s*https?://(www\.)?vimeo.com/([a-zA-Z0-9]+)(#.*)?\s*$|', $mediaUrl, $matches);
if ($matchFound) {
return (object)array(
'mediaUrl' => $mediaUrl,
'mediaSource' => 'vimeo',
'mediaID' => $matches[2],
);
}
// Check for Soundcloud
$matchFound = preg_match('|^\s*https?://(www\.)?soundcloud.com/([a-zA-Z0-9\-/]+)(#.*)?\s*$|', $mediaUrl, $matches);
if ($matchFound) {
return (object)array(
'mediaUrl' => $mediaUrl,
'mediaSource' => 'soundcloud',
'mediaID' => $matches[2],
);
}
// Assume some general media source
return (object)array(
'mediaUrl' => $mediaUrl,
'mediaSource' => 'other',
'mediaID' => $mediaUrl,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment