How can I grab the video ID only from the youtube’s URLs?
For instance,
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
You should never use regular expressions when the same thing can be accomplished through purpose-built functions.
You can use
parse_urlto break the URL up into its segments, andparse_strto break the query string portion into a key/value array:The alternate form of
parse_strextracts variables into the current scope. You could build this into a function to find and return thevparameter: