Skip to content

Instantly share code, notes, and snippets.

@lutzissler
Last active February 14, 2017 11:53
Show Gist options
  • Select an option

  • Save lutzissler/11250517 to your computer and use it in GitHub Desktop.

Select an option

Save lutzissler/11250517 to your computer and use it in GitHub Desktop.
Takes a Vimeo or YouTube URL and returns the iframe embed URL. Other URLs remain untouched.
function get_video_embed_url($url) {
// Check for YouTube
$matchFound = preg_match('/^(https?:\/\/(www\.)?youtube\.com\/watch\?(t=(.+)&)?v=|https?:\/\/youtu\.be\/)([^?]+)(\?(t=(.+))?.*)?$/', $url, $matches);
if ($matchFound) {
// Video is a Youtube video
$url = '//www.youtube.com/embed/' . $matches[5];
if ($matches[4]) {
$url = $url . '?start=' . $matches[4];
} else if ($matches[8]) {
$url = $url . '?start=' . $matches[8];
}
} else {
// Check for Vimeo
$matchFound = preg_match('#^https?://vimeo\.com/([a-zA-Z0-9]+)/?$#', $url, $matches);
if ($matchFound) {
// Video is a Vimeo video
$url = '//player.vimeo.com/video/' . $matches[1] . '?byline=0&portrait=0';
}
}
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment