Skip to content

Instantly share code, notes, and snippets.

@henrytran9x
Created March 1, 2015 15:36
Show Gist options
  • Save henrytran9x/8bf172b434b2705834c1 to your computer and use it in GitHub Desktop.
Save henrytran9x/8bf172b434b2705834c1 to your computer and use it in GitHub Desktop.
Check & Parse YouTube & Vimeo Video IDs
<?php
/*
* Check if URL is YouTube
*/
function _is_youtube($url)
{
return (preg_match('/youtu\.be/i', $url) || preg_match('/youtube\.com\/watch/i', $url));
}
/*
* Check if URL is Vimeo
*/
function _is_vimeo($url)
{
return (preg_match('/vimeo\.com/i', $url));
}
/*
* Get the YouTube Video ID from a URL
*/
function _youtube_video_id($url)
{
if(_is_youtube($url))
{
$pattern = '/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/';
preg_match($pattern, $url, $matches);
if (count($matches) && strlen($matches[7]) == 11)
{
return $matches[7];
}
}
return '';
}
/*
* Get the Vimeo Video ID from a URL
*/
function _vimeo_video_id($url)
{
if(_is_vimeo($url))
{
$pattern = '/\/\/(www\.)?vimeo.com\/(\d+)($|\/)/';
preg_match($pattern, $url, $matches);
if (count($matches))
{
return $matches[2];
}
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment