Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created August 28, 2017 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/82fb6f24599f95501d36b79a360ac8b1 to your computer and use it in GitHub Desktop.
Save billerickson/82fb6f24599f95501d36b79a360ac8b1 to your computer and use it in GitHub Desktop.
<?php
/**
* Get YouTube thumbnail from video url
*
*/
function be_youtube_thumbnail( $video_url ) {
$y_id = preg_replace( '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i', '$1', $video_url );
$image = $y_id ? '<img src="https://img.youtube.com/vi/' . $y_id . '/0.jpg" />' : false;
return $image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment