Check & Parse YouTube & Vimeo Video IDs

Public

Check if URL is YouTube

</> CopyGet raw version
php
  1. function _is_youtube($url)
  2. {
  3. return (preg_match('/youtu\.be/i', $url) || preg_match('/youtube\.com\/watch/i', $url));
  4. }

Check if URL is Vimeo

</> CopyGet raw version
php
  1. function _is_vimeo($url)
  2. {
  3. return (preg_match('/vimeo\.com/i', $url));
  4. }

Get the YouTube Video ID from a URL

</> CopyGet raw version
php
  1. _youtube_video_id($url)
  2. {
  3. if(_is_youtube($url))
  4. {
  5. $pattern = '/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/';
  6. preg_match($pattern, $url, $matches);
  7. if (count($matches) && strlen($matches[7]) == 11)
  8. {
  9. return $matches[7];
  10. }
  11. }
  12.  
  13. return '';
  14. }

Get the Vimeo Video ID from a URL

</> CopyGet raw version
php
  1. function _vimeo_video_id($url)
  2. {
  3. if(_is_vimeo($url))
  4. {
  5. $pattern = '/\/\/(www\.)?vimeo.com\/(\d+)($|\/)/';
  6. preg_match($pattern, $url, $matches);
  7. if (count($matches))
  8. {
  9. return $matches[2];
  10. }
  11. }
  12.  
  13. return '';
  14. }

Comments

smit's picture

Thanks.. This what i was looking for!

ibrahim's picture

thanks for tutorial
could you write a tutorial how to Check & Parse dailymotion url and id.

Deepak's picture

Superb tutorial... really very helpfull.