Forked from yoanmarchal/youtube-vimeo-embed-urls.php
Last active
February 9, 2020 07:30
-
-
Save ashokmhrj/30fa9b063ff4a7cf9d073df9529ab842 to your computer and use it in GitHub Desktop.
PHP Function to Convert Youtube and Vimeo URLs to Lightbox-Ready Equivalents
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 | |
/* Forked from : https://gist.github.com/yoanmarchal/4030827 */ | |
/** | |
* Given a string containing any combination of YouTube and Vimeo video URLs in | |
* a variety of formats (iframe, shortened, etc), each separated by a line break, | |
* parse the video string and determine it's valid embeddable URL for usage in | |
* popular JavaScript lightbox plugins. | |
* | |
* In addition, this handler grabs both the maximize size and thumbnail versions | |
* of video images for your general consumption. In the case of Vimeo, you must | |
* have the ability to make remote calls using file_get_contents(), which may be | |
* a problem on shared hosts. | |
* | |
* Data gets returned in the format: | |
* | |
* array( | |
* array( | |
* 'url' => 'http://path.to/embeddable/video', | |
* 'thumbnail' => 'http://path.to/thumbnail/image.jpg', | |
* 'fullsize' => 'http://path.to/fullsize/image.jpg', | |
* 'embed' => '<iframe src ='' ></iframe>' | |
* ) | |
* ) | |
* | |
* @param string $videoString | |
* @return array An array of video metadata if found | |
* | |
* @author Corey Ballou http://coreyballou.com | |
* @copyright (c) 2012 Skookum Digital Works http://skookum.com | |
* @license | |
*/function parseVideos($videoString = null) | |
{ | |
// return data | |
$videos = array(); | |
if (!empty($videoString)) { | |
// split on line breaks | |
$videoString = stripslashes(trim($videoString)); | |
$videoString = explode("\n", $videoString); | |
$videoString = array_filter($videoString, 'trim'); | |
// check each video for proper formatting | |
foreach ($videoString as $video) { | |
// check for iframe to get the video url | |
if (strpos($video, 'iframe') !== FALSE) { | |
// retrieve the video url | |
$anchorRegex = '/src="(.*)?"/isU'; | |
$results = array(); | |
if (preg_match($anchorRegex, $video, $results)) { | |
$link = trim($results[1]); | |
} | |
} else { | |
// we already have a url | |
$link = $video; | |
} | |
echo 'link '. $link .PHP_EOL; | |
// if we have a URL, parse it down | |
if (!empty($link)) { | |
// initial values | |
$video_id = NULL; | |
$videoIdRegex = NULL; | |
$results = array(); | |
// check for type of youtube link | |
if (strpos($link, 'youtu') !== FALSE) { | |
if (strpos($link, 'youtube.com') !== FALSE) { | |
// works on: | |
# https://www.youtube.com/watch?v=DMRRC0rwO_I | |
// http://www.youtube.com/embed/VIDEOID | |
// http://www.youtube.com/embed/VIDEOID?modestbranding=1&rel=0 | |
// http://www.youtube.com/v/VIDEO-ID?fs=1&hl=en_US | |
$videoIdRegex = '/youtube.com\/((?:embed)|(?:watch)|(?:v))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i'; | |
#$videoIdRegex = '/youtube.com\/(?:embed|watch|v){1}\/([a-zA-Z0-9_]+)\??/i'; | |
} else if (strpos($link, 'youtu.be') !== FALSE) { | |
// works on: | |
// http://youtu.be/daro6K6mym8 | |
$videoIdRegex = '/youtu.be\/([a-zA-Z0-9_]+)\??/i'; | |
} | |
if ($videoIdRegex !== NULL) { | |
if ( preg_match( $videoIdRegex, $link, $results ) ) { | |
$video_str = 'http://www.youtube.com/v/%s?fs=1&autoplay=1'; | |
$thumbnail_str = 'http://img.youtube.com/vi/%s/2.jpg'; | |
$fullsize_str = 'http://img.youtube.com/vi/%s/0.jpg'; | |
$video_id = $results[1]; | |
foreach( $results as $key => $result ) { | |
if( 0 == $key ) continue; | |
if( $result != 'embed' && $result != 'watch' && $result != 'v' && $result != '?v=' && $result != '/' ) { | |
$video_id = $results[$key]; | |
break; | |
} | |
} | |
echo "<pre>"; | |
print_r( $results ); | |
} | |
} | |
} | |
// handle vimeo videos | |
else if (strpos($video, 'vimeo') !== FALSE) { | |
if (strpos($video, 'player.vimeo.com') !== FALSE) { | |
// works on: | |
// http://player.vimeo.com/video/37985580?title=0&byline=0&portrait=0 | |
$videoIdRegex = '/player.vimeo.com\/video\/([0-9]+)\??/i'; | |
} else { | |
// works on: | |
// http://vimeo.com/37985580 | |
$videoIdRegex = '/vimeo.com\/([0-9]+)\??/i'; | |
} | |
if ($videoIdRegex !== NULL) { | |
if (preg_match($videoIdRegex, $link, $results)) { | |
$video_id = $results[1]; | |
// get the thumbnail | |
try { | |
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video_id.php")); | |
if (!empty($hash) && is_array($hash)) { | |
$video_str = 'http://vimeo.com/moogaloop.swf?clip_id=%s'; | |
$thumbnail_str = $hash[0]['thumbnail_small']; | |
$fullsize_str = $hash[0]['thumbnail_large']; | |
} else { | |
// don't use, couldn't find what we need | |
unset($video_id); | |
} | |
} catch (Exception $e) { | |
unset($video_id); | |
} | |
} | |
} | |
} | |
// check if we have a video id, if so, add the video metadata | |
if (!empty($video_id)) { | |
// add to return | |
$videos[] = array( | |
'id'=> $video_id, | |
'url' => sprintf($video_str, $video_id), | |
'thumbnail' => sprintf($thumbnail_str, $video_id), | |
'fullsize' => sprintf($fullsize_str, $video_id), | |
'embeded' => sprintf("<iframe src=\"//www.youtube.com/embed/%s\" allowfullscreen></iframe>", $video_id) | |
); | |
} | |
} | |
} | |
} | |
// return array of parsed videos | |
return $videos; | |
} | |
/* | |
call function | |
var_dump( parseVideos($url) ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment