Skip to content

Instantly share code, notes, and snippets.

@quodos
Forked from jlamim/youtube-vimeo-embed-urls.php
Last active August 29, 2015 14:23
Show Gist options
  • Save quodos/1179fef12dcf4acd13f9 to your computer and use it in GitHub Desktop.
Save quodos/1179fef12dcf4acd13f9 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'
* )
* )
*
* This is a fork of Corey Ballou script (jlamim/youtube-vimeo-embed-urls.php).
* I removed the thumbnail features and added a new youtube parse regex to allow
* every url format for youtube videos currently present. Also I changed the
* youtube embed url.
*
* @param string $videoString
* @return array An array of video metadata if found
*
* @author Corey Ballou <http://coreyballou.com>, Thomas Wilhelm <http://thomaswilhelm.name>
* @copyright author
* @license MIT
*/
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) {
$videoIdRegex = '/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*>|<\/a>))[?=&+%\w.-]*/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 = 'https://www.youtube.com/embed/%s?controls=0';
$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];
$video_str = 'http://vimeo.com/moogaloop.swf?clip_id=%s';
}
}
}
// 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)
);
}
}
}
}
// return array of parsed videos
return $videos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment