Created
August 21, 2014 17:56
-
-
Save infn8/c24414961114a317e6b4 to your computer and use it in GitHub Desktop.
Parse youtube or vimeo URLs
This file contains 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
function parse_video_link($link){ | |
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $link, $match)) { | |
$video_id = $match[1]; | |
return array("video" => "youtube", "videoID" => $video_id); | |
} | |
$regexstr = '~ | |
# Match Vimeo link and embed code | |
(?:<iframe [^>]*src=")? # If iframe match up to first quote of src | |
(?: # Group vimeo url | |
https?:\/\/ # Either http or https | |
(?:[\w]+\.)* # Optional subdomains | |
vimeo\.com # Match vimeo.com | |
(?:[\/\w]*\/videos?)? # Optional video sub directory this handles groups links also | |
\/ # Slash before Id | |
([0-9]+) # $1: VIDEO_ID is numeric | |
[^\s]* # Not a space | |
) # End group | |
"? # Match end quote if part of src | |
(?:[^>]*></iframe>)? # Match the end of the iframe | |
(?:<p>.*</p>)? # Match any title information stuff | |
~ix'; | |
if(preg_match($regexstr, $link, $match)){ | |
$video_id = $match[1]; | |
return array("video" => "vimeo", "videoID" => $video_id); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment