Created
January 28, 2017 02:29
-
-
Save vaporic/32cc894da95c9b6b9338d8e3628d0d1d to your computer and use it in GitHub Desktop.
Parse Video Youtube & Vimeo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function parseVideo(url) { | |
// - Supported YouTube URL formats: | |
// - http://www.youtube.com/watch?v=My2FRPA3Gf8 | |
// - http://youtu.be/My2FRPA3Gf8 | |
// - https://youtube.googleapis.com/v/My2FRPA3Gf8 | |
// - Supported Vimeo URL formats: | |
// - http://vimeo.com/25451551 | |
// - http://player.vimeo.com/video/25451551 | |
// - Also supports relative URLs: | |
// - //player.vimeo.com/video/25451551 | |
url.match(/(http:\/\/|https:\/\/|)(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/); | |
if (RegExp.$3.indexOf('youtu') > -1) { | |
var type = 'youtube'; | |
} else if (RegExp.$3.indexOf('vimeo') > -1) { | |
var type = 'vimeo'; | |
} | |
return { | |
type: type, | |
id: RegExp.$6 | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment