Skip to content

Instantly share code, notes, and snippets.

@derUli
Last active April 5, 2018 16:41
Show Gist options
  • Save derUli/b9325a1a7700331c9c320fcf28610f9b to your computer and use it in GitHub Desktop.
Save derUli/b9325a1a7700331c9c320fcf28610f9b to your computer and use it in GitHub Desktop.
Generate Youtube Responsive Video Player Embed HTML Code with PHP
<?php
function getYoutubeEmbedHtml($url){
$query = parse_url($url, PHP_URL_QUERY );
$args = array();
parse_str($query, $args);
$videoId = isset($args["v"]) ? $args["v"] : null;
if(!$videoId){
return null;
}
return "<style>.iframe-container { position:relative; margin-bottom: 30px; padding-bottom:56.25%; padding-top:25px; height:0; max-width:100%; } .iframe-container iframe { position:absolute; top:0; left:0; width:100%; height:100%; border:none; }</style><div class=\"iframe-container\"><iframe src=\"https://youtube.com/embed/{videoId}\" allowfullscreen=\"\"></iframe><br /></div>";
}
echo getYoutubeEmbedHtml("https://www.youtube.com/watch?v=6zbsUtQL4nY");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment