Skip to content

Instantly share code, notes, and snippets.

@rungta
Forked from cballou/youtube-vimeo-embed-urls.php
Last active October 14, 2016 08:11
Show Gist options
  • Save rungta/9764293 to your computer and use it in GitHub Desktop.
Save rungta/9764293 to your computer and use it in GitHub Desktop.
Now returns video metadata instead of an embeddable URL
<?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 host and ID.
*
* Data gets returned in the format:
*
* array(
* array(
* 'host' => 'youtube' | 'vimeo',
* 'id' => <video_id>,
* )
* )
*
* @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
*
* @modified Prateek Rungta http://prateekrungta.com
* - Improved YouTube extraction
* - Dropped thumbnail referencing/fetching
* - Now returns the video metadata instead of generating a URL
*
*/
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;
$video_host = NULL;
$videoIdRegex = NULL;
$results = array();
// check for type of youtube link
if (strpos($link, 'youtu') !== FALSE) {
if (strpos($link, 'youtube.com') !== FALSE) {
if (strpos($link, 'watch') !== FALSE) {
// works on:
// http://www.youtube.com/watch?feature=player_embedded&v=VIDEOID#!
$videoIdRegex = '!v=([a-z0-9_-]+)!i';
} else {
// 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_id = $results[1];
$video_host = 'youtube';
}
}
}
// 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_host = 'vimeo';
}
}
}
// check if we have a video id, if so, add the video metadata
if (!empty($video_id)) {
// add to return
$videos[] = array(
'host' => $video_host,
'id' => $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