Skip to content

Instantly share code, notes, and snippets.

@azizultex
Created April 24, 2017 17:21
Show Gist options
  • Save azizultex/9fa0eafd08f61c0586ace5598475cc76 to your computer and use it in GitHub Desktop.
Save azizultex/9fa0eafd08f61c0586ace5598475cc76 to your computer and use it in GitHub Desktop.
Generate Youtube and Vimeo Video Embed URL dynamically
/**
* get youtube video ID from URL
* http://stackoverflow.com/questions/6556559/youtube-api-extract-video-id
* @param string $url
* @return string Youtube video id or FALSE if none found.
*/
function youtube_id_from_url($url) {
$pattern =
'%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x'
;
$result = preg_match($pattern, $url, $matches);
if ($result) {
return $matches[1];
}
return false;
}
function vimeo_id_from_url($url) {
if(preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/", $url, $output_array)) {
return $output_array[5];
}
}
function generate_video_embed_url($url) {
$output = '';
if (strpos($url, 'youtube') > 0) {
$output = 'https://www.youtube.com/embed/' . youtube_id_from_url($url);
} elseif (strpos($url, 'vimeo') > 0) {
$output ='https://player.vimeo.com/video/'. vimeo_id_from_url($url) .'?title=0&byline=0';
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment