Last active
February 14, 2017 11:53
-
-
Save lutzissler/11250517 to your computer and use it in GitHub Desktop.
Takes a Vimeo or YouTube URL and returns the iframe embed URL. Other URLs remain untouched.
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
| function get_video_embed_url($url) { | |
| // Check for YouTube | |
| $matchFound = preg_match('/^(https?:\/\/(www\.)?youtube\.com\/watch\?(t=(.+)&)?v=|https?:\/\/youtu\.be\/)([^?]+)(\?(t=(.+))?.*)?$/', $url, $matches); | |
| if ($matchFound) { | |
| // Video is a Youtube video | |
| $url = '//www.youtube.com/embed/' . $matches[5]; | |
| if ($matches[4]) { | |
| $url = $url . '?start=' . $matches[4]; | |
| } else if ($matches[8]) { | |
| $url = $url . '?start=' . $matches[8]; | |
| } | |
| } else { | |
| // Check for Vimeo | |
| $matchFound = preg_match('#^https?://vimeo\.com/([a-zA-Z0-9]+)/?$#', $url, $matches); | |
| if ($matchFound) { | |
| // Video is a Vimeo video | |
| $url = '//player.vimeo.com/video/' . $matches[1] . '?byline=0&portrait=0'; | |
| } | |
| } | |
| return $url; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment