Skip to content

Instantly share code, notes, and snippets.

@davist11
Created May 9, 2014 18:56
Show Gist options
  • Save davist11/2b3488fe9587be959a46 to your computer and use it in GitHub Desktop.
Save davist11/2b3488fe9587be959a46 to your computer and use it in GitHub Desktop.
Generate video embed URL from YouTube or Vimeo
<?php
public function getEmbedCode($url)
{
if (strpos($url, 'youtube') !== FALSE) {
$url_parts = parse_url($url);
parse_str($url_parts['query'], $segments);
return '//www.youtube.com/embed/' . $segments['v'];
} else if (strpos($url, 'vimeo') !== FALSE) {
$url_parts = parse_url($url);
$segments = explode('/', $url_parts['path']);
return '//player.vimeo.com/video/' . $segments[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment