Reputation: 119
My site uses javascript which reads youtube video ID's from an array. In the format:
1: 'A8JsRx2loix',
2: 'XFGAQrEUaes',
3: 'qX9FSZJu44s'
Whats the best way to take a list of urls like
http://www.youtube.com/watch?feature=player_detailpage&v=bCJ-qARb2M0
http://www.youtube.com/watch?feature=player_detailpage&v=bCJ-qAswdM2
http://www.youtube.com/watch?feature=player_detailpage&v=bCJ-sh7b2M0
and extract the 11 digit video ids, putting them in the given format above.
NOTE: the urls can be in different formats, for example when they are in playlists.
Upvotes: 0
Views: 486
Reputation: 92641
You can use this function which should deal with any youtube url, It will return an object like:
{
type: 'youtube',
id: 'A8JsRx2loix'
}
function testUrlForMedia(url) {
var success = false;
var media = {};
if (url.match('http://(www.)?youtube|youtu\.be')) {
if (url.match('embed')) {
youtube_id = url.split(/embed\//)[1].split('"')[0];
}
else {
youtube_id = url.split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0];
}
media.type = "youtube";
media.id = youtube_id;
success = true;
}
if (success) {
return media;
}
return false;
}
Upvotes: 1