Search This Blog

Loading...

Saturday, December 3, 2011

Grab the video ID only from youtube's URLs

I want to get the v=id from youtube's URL with javascript(no jquery, pure javascript);
Example Youtube URL formats:

http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW
http://www.youtube.com/watch?v=u8nQa1cJyX8
or any other youtube format what contains a video id in the url
Result from these formats

u8nQa1cJyX8
Solution:
// Returns null if video id doesn't exist in URL
function get_video_id($url) {
  $parts = parse_url($url);

  // Make sure $url had a query string
  if (!array_key_exists('query', $parts))
    return null;

  parse_str($parts['query']);

  // Return the 'v' parameter if it existed
  return isset($v) ? $v : null;
}
http://stackoverflow.com/questions/4045154/grab-the-video-id-only-from-youtubes-urls
http://snipplr.com/view/19232/

0 comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails