Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created December 18, 2019 11:46
Show Gist options
  • Select an option

  • Save phpfiddle/1f9db6b426681301f265c4cbeba9abba to your computer and use it in GitHub Desktop.

Select an option

Save phpfiddle/1f9db6b426681301f265c4cbeba9abba to your computer and use it in GitHub Desktop.
[ Posted by Hiren ] Embed link for youtube and vimeo
<?php
$embed = "";
$thumb = "";
$url = $resource->video;
if (strpos($url, 'youtube') > 0)
{
$embed = 'https://www.youtube.com/embed/';
// Now get the 'v=' value & stick on the end
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $matches);
$v = $matches[1];
// Final result
$embed = $embed . $v;
$thumb = "https://img.youtube.com/vi/$v/mqdefault.jpg";
}
elseif (strpos($url, 'vimeo') > 0)
{
$embed = 'https://player.vimeo.com/video/';
// Now get the last part of the url
$v = substr( strrchr( $url, '/' ), 1 );
// Final result
$embed = $embed . $v;
$vimeo = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$v.php"));
$thumb = $vimeo[0]['thumbnail_medium'];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment