video ID filter, PHP code, video URL, YouTube video ID, retrieve poster image
function get_youtube_id($url) {
$pattern = '%^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=))([\w-]{10,12})$%x';
$result = preg_match($pattern, $url, $matches);
if (false !== $result) {
return $matches[1];
}
return false;
}
echo get_youtube_id('http://www.youtube.com/watch?v=dQw4w9WgXcQ');
echo get_youtube_id('http://youtu.be/dQw4w9WgXcQ');
$video_id = get_youtube_id('http://youtu.be/dQw4w9WgXcQ');
echo '<img src="http://img.youtube.com/vi/' . $video_id . '/hqdefault.jpg" alt="">';