Skip to content

Instantly share code, notes, and snippets.

@nilesolutions
Created December 23, 2017 11:43
Show Gist options
  • Save nilesolutions/a04c0f941a3aeafb67a87a40fc5e919c to your computer and use it in GitHub Desktop.
Save nilesolutions/a04c0f941a3aeafb67a87a40fc5e919c to your computer and use it in GitHub Desktop.
<?php
/**
* Youtube link embed conversion
*
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
*
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
* https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
*
*/
$video = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","//www.youtube.com/embed/$1", $youtube_link);
$thumb = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","//img.youtube.com/vi/$1/maxresdefault.jpg", $youtube_link);
// no controls
$youtube_embed = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","//www.youtube.com/embed/$1?rel=0&amp;controls=0&amp;showinfo=0", $youtube_link);
?>
<iframe width="100%" height="400" src="<?=$video?>" frameborder="0" allowfullscreen></iframe>
<img src="<?=$thumb?>" alt="" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment