1

How can I grab the video ID only from the youtube's URLs?

For instance,

http://www.youtube.com/watch?v=aPm3QVKlBJg

sometime the URLs contain other information after the 'v' like

http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index

but I don't want the other info, just video ID.

I only can think of using explode,

$url  = "http://www.youtube.com/watch?v=aPm3QVKlBJg";
$pieces = explode("v=", $url);

but how to clean up the URLs like this?

http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index

Run
  • 54,938
  • 169
  • 450
  • 748
  • possible duplicate of [parse youtube video id using preg_match](http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match) – kennytm Oct 28 '10 at 16:50
  • This is *not* a duplicate unless you assume the user was after a regex solution, which is *not* the best solution to this problem. – user229044 Oct 28 '10 at 17:00
  • @meagar but that question also had a `parse_url` solution by Gumbo, so it'll work as a duplicate. – Pekka Oct 28 '10 at 17:05
  • @Pekka They're asking different questions. The regex solution was accepted as correct in the other question, seemingly indicating it really was about a regex, not about the *best* method of finding the video id. – user229044 Oct 28 '10 at 17:13
  • @meagar true, that's a point. The same happened to my answer in this question: http://stackoverflow.com/questions/3737634/ But now we have one whose accepted answer is actually the correct one. Yay! :) – Pekka Oct 28 '10 at 17:14
  • yes the answer from the other thread is not what I am looking for. thanks guys! :-) – Run Oct 28 '10 at 17:21

1 Answers1

10

You should never use regular expressions when the same thing can be accomplished through purpose-built functions.

You can use parse_url to break the URL up into its segments, and parse_str to break the query string portion into a key/value array:

$url = 'http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index'

// break the URL into its components
$parts = parse_url($url);

// $parts['query'] contains the query string: 'v=Z29MkJdMKqs&feature=grec_index'

// parse variables into key=>value array
$query = array();
parse_str($parts['query'], $query);

echo $query['v']; // Z29MkJdMKqs
echo $query['feature'] // grec_index

The alternate form of parse_str extracts variables into the current scope. You could build this into a function to find and return the v parameter:

// 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;
}
user229044
  • 232,980
  • 40
  • 330
  • 338
  • The problem with this solution is that YouTube URLs are inconsistent to say the least. There are at least 10 different URL formats that YouTube will accept and only a few of them use the ?v= format for submitting the video ID. Most URL formats contain the video ID as part of the URLs path as follows: `http://www.youtube.com/e/dQw4w9WgXcQ` – Benjam Sep 09 '11 at 02:01