Skip to content

Instantly share code, notes, and snippets.

@derUli
derUli / youtube-html-generate-responsive.php
Last active April 5, 2018 16:41
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>";