-
-
Save cballou/2217372 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* 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', | |
* ) | |
* ) | |
* | |
* @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; | |
} | |
// 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: | |
// 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|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]; | |
} | |
} | |
} | |
// 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( | |
'url' => sprintf($video_str, $video_id), | |
'thumbnail' => sprintf($thumbnail_str, $video_id), | |
'fullsize' => sprintf($fullsize_str, $video_id) | |
); | |
} | |
} | |
} | |
} | |
// return array of parsed videos | |
return $videos; | |
} |
I noticed it doesn't work with video links with the following format https://youtu.be/VidE0c0DE
Replace the line 73
$videoIdRegex = '/youtube.com/(?:embed|v){1}/([a-zA-Z0-9_]+)??/i';
to
$videoIdRegex = "/^(?:http(?:s)?://)?(?:www.)?(?:m.)?(?:youtu.be/|youtube.com/(?:(?:watch)??(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)/))([^\?&\"'>]+)/";
Links working:
youtube.com/v/vidid
youtube.com/vi/vidid
youtube.com/?v=vidid
youtube.com/?vi=vidid
youtube.com/watch?v=vidid
youtube.com/watch?vi=vidid
youtu.be/vidid
youtube.com/embed/vidid
http://youtube.com/v/vidid
http://www.youtube.com/v/vidid
https://www.youtube.com/v/vidid
youtube.com/watch?v=vidid&wtv=wtv
http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
https://m.youtube.com/watch?v=vidid
Sorry, but it does not work ...
Thanks,
It's working like charm
Hi Friend!
Thanks for that!
There are some url is not working... Following
http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
Cya