Skip to content

Instantly share code, notes, and snippets.

@santanup789
Created July 2, 2021 06:57
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 santanup789/1474c7593c0f4926b0f8145659ce6ee0 to your computer and use it in GitHub Desktop.
Save santanup789/1474c7593c0f4926b0f8145659ce6ee0 to your computer and use it in GitHub Desktop.
Get Youtube video thumbnail and title dynamically using video ID
<?php
$get_yt_url = 'https://www.youtube.com/watch?v=VaSstf9VJGQ';
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $get_yt_url, $match);
$youtube_id = $match[1];
echo "<img src='https://img.youtube.com/vi/$youtube_id/maxresdefault.jpg' alt='$getTitle'>";
//Get youtube video title dynamically
echo explode(' - YouTube',explode('</title>',explode('<title>',file_get_contents("https://www.youtube.com/watch?v=$youtube_id"))[1])[0])[0];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment