You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functiononPlayerReady(event){// uncomment below if you want the video to start playing as soon as it loads// you can also add autoplay: 1 to playerVars instead of doing this// event.target.playVideo();}functiononPlayerStateChange(event){console.log(event.data);if(YT.PlayerState.ENDED==event.data){console.log('Video end');_gaq.push(['_trackEvent','video','Completed',event.target.getVideoUrl()]);}}functioninitYouTubeVideos(){varyoutubePlayers=$('.youtube-player');if(false===youtubePlayers.length>0){return;}youtubePlayers.each(function(){varplayer=$(this);varplayerID=player.attr('id');varvideoID=player.data('videoid');newYT.Player(playerID,{height: '390',width: '640',videoId: videoID,// don't show related, this accepts any params an embed would// see https://developers.google.com/youtube/player_parametersplayerVars: {rel: 0},events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});});}functiononYouTubePlayerAPIReady(){initYouTubeVideos();}
Example PHP function to extract a YouTube Video ID from a string
/** * Attempts to extract a youtube video ID from strings user might input */functionget_youtube_vid_id($link) {
$delimiters = array(
'watch?v=',
'youtu.be/',
'embed/',
'youtube.com/v/'
);
$vid_id = false;
foreach ($delimitersas$delimiter) {
if (false !== stripos($link, $delimiter)) {
$link = explode($delimiter, $link);
# Remove any params coming after "v"if (false !== strpos($link[1], '&')) {
$params = explode('&', $link[1]);
$vid_id = $params[0];
} else {
$vid_id = $link[1];
}
break;
}
}
if ($vid_id) {
return$vid_id;
}
return'';
}