Skip to content

Instantly share code, notes, and snippets.

@noomz
Created September 23, 2016 08:33
Show Gist options
  • Save noomz/0d41014788f222f34553ffb58c66f7ae to your computer and use it in GitHub Desktop.
Save noomz/0d41014788f222f34553ffb58c66f7ae to your computer and use it in GitHub Desktop.
Show youtube embed as width 100%
<?php
$youtube_url = 'https://www.youtube.com/watch?v=UeR_jEx6Pzs';
// or
$youtube_url = '<iframe width="560" height="315" src="https://www.youtube.com/embed/UeR_jEx6Pzs" frameborder="0" allowfullscreen></iframe>';
if ($youtube_url) {
if (preg_match('/iframe/', $youtube_url)) {
$iframe_url = preg_replace('/width=[\"\'].*?[\"\']/', 'width="100%"', $youtube_url);
$iframe_url = preg_replace('/height=[\"\'].*?[\"\']/', 'height="100%"', $iframe_url);
$fixed = $iframe_url;
}
else {
$video_id = preg_replace("/.*?[\&\?]v=([a-zA-Z0-9_]+)?.*/", "$1", $youtube_url);
$fixed = '<iframe width="100%" height="100%" src="https://www.youtube.com/embed/' . $video_id . '" frameborder="0" allowfullscreen></iframe>';
}
}
print $fixed;
.youtube-wrapper {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%;
margin: 0 0 15px;
iframe {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment