Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Function to Convert Youtube and Vimeo URLs to Lightbox-Ready Equivalents
<?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&amp;rel=0
// http://www.youtube.com/v/VIDEO-ID?fs=1&amp;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&amp;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&amp;byline=0&amp;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;
}
@yoanmarchal

This comment has been minimized.

Show comment
Hide comment
@yoanmarchal

yoanmarchal Nov 7, 2012

hi your function is very practical but it's seem that it's doesn't work on video with "&feature=related" at the end
exemple:"http://www.youtube.com/watch?v=ULHHQiFfK9o&feature=related"
could you help me ?

yoanmarchal commented Nov 7, 2012

hi your function is very practical but it's seem that it's doesn't work on video with "&feature=related" at the end
exemple:"http://www.youtube.com/watch?v=ULHHQiFfK9o&feature=related"
could you help me ?

@jlamim

This comment has been minimized.

Show comment
Hide comment
@jlamim

jlamim Jul 23, 2014

Yoanmarchal, in my fork of this Gist has the solution for this url format.

jlamim commented Jul 23, 2014

Yoanmarchal, in my fork of this Gist has the solution for this url format.

@kressly

This comment has been minimized.

Show comment
Hide comment
@kressly

kressly Feb 17, 2015

How to use this
I have tried

$text = "https://www.youtube.com/watch?v=WIpe1OVbQpU";

echo parseVideos($text);

But it did not work

kressly commented Feb 17, 2015

How to use this
I have tried

$text = "https://www.youtube.com/watch?v=WIpe1OVbQpU";

echo parseVideos($text);

But it did not work

@gr33ndata

This comment has been minimized.

Show comment
Hide comment
@gr33ndata

gr33ndata Sep 17, 2015

I noticed it doesn't work with video links with the following format https://youtu.be/VidE0c0DE

gr33ndata commented Sep 17, 2015

I noticed it doesn't work with video links with the following format https://youtu.be/VidE0c0DE

@robertorotondo

This comment has been minimized.

Show comment
Hide comment
@robertorotondo

robertorotondo Oct 16, 2015

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

robertorotondo commented Oct 16, 2015

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

@kamilsj

This comment has been minimized.

Show comment
Hide comment
@kamilsj

kamilsj Oct 19, 2015

Sorry, but it does not work ...

kamilsj commented Oct 19, 2015

Sorry, but it does not work ...

@shahmanthan9

This comment has been minimized.

Show comment
Hide comment
@shahmanthan9

shahmanthan9 May 24, 2016

Thanks,
It's working like charm

shahmanthan9 commented May 24, 2016

Thanks,
It's working like charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment